一,xml配置文件版bean注入
1,新建一个maven工程,导入依赖,pom文件如下:
<?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.dream</groupId> <artifactId>spring-study001</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.13.RELEASE</version> </dependency> </dependencies> </project> |
2,新建一个Student类,代码如下:
package com.dream.bean; public class Student { private String name; private Integer age; public Student(String name, Integer age) { this.name = name; this.age = age; } public Student() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } } |
3,配置文件版显示bean注入,配置文件为beans.xml:
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="student" class="com.dream.bean.Student"> <property name="age" value="18"></property> <property name="name" value="lisi"></property> </bean> </beans> |
4,测试配置文件注入bean
package com.dream; import com.dream.bean.Student; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Student student=(Student)applicationContext.getBean("student"); System.out.print(student); } } |
5,测试结果
信息: Loading XML bean definitions from class path resource [beans.xml] Student{name='lisi', age=18} Process finished with exit code 0 |
二,注解版bean注入
1,新建配置类
package com.dream.config; import com.dream.bean.Student; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //配置类(等价原来的配置文件) @Configuration public class BeansConfig { //给容器中注册一个bean;类型为返回值类型,id默认是方法名作为id @Bean public Student student(){ return new Student("王五",28); } } |
2,测试
package com.dream; import com.dream.bean.Student; import com.dream.config.BeansConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { public static void main(String[] args) { ApplicationContext applicationContext=new AnnotationConfigApplicationContext(BeansConfig.class); Student bean = applicationContext.getBean(Student.class); System.out.print("获取对象:"+bean ); } private static void xmlBean() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); Student student=(Student)applicationContext.getBean("student"); System.out.print(student); } } |
3,测试结果
获取对象:Student{name='王五', age=28} |