SpringIoc容器 作业3-2
来源:3-2 自由编程
rudtjd
2023-03-02 16:23:36
package com.imooc.spring.ioc.entity;
public class Cat {
private String name;
private String age;
public Cat() {
}
public Cat(String name,
String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}package com.imooc.spring.ioc.entity;
public class Dog {
private String name;
private String age;
public Dog() {
}
public Dog(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.imooc.spring</groupId> <artifactId>ioc</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.6.RELEASE</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
package com.imooc.spring.ioc;
import com.imooc.spring.ioc.entity.Cat;
import com.imooc.spring.ioc.entity.Dog;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:ApplicationContext.xml");
Cat Cat = context.getBean("Cat",Cat.class);
Dog Dog = context.getBean("Dog",Dog.class);
System.out.println(Cat.toString());
System.out.println(Dog.toString());
}
}<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="Cat" class="com.imooc.spring.ioc.entity.Cat"> <property name="name" value="喵喵"></property> <property name="age" value="2"></property> </bean> <bean id="Dog" class="com.imooc.spring.ioc.entity.Dog"> <property name="name" value="旺旺"></property> <property name="age" value="5"></property> </bean> </beans>
1回答
同学你好,测试同学代码已完成练习,棒棒哒!继续加油!
祝学习愉快!
相似问题
回答 1