前言
使用xml文件配置和使用sping注解方式来创建javaBean
向ioc容器中注入javaBean,并获取java对象
准备环境
1.创建一个maven工程(spring-annotation),我这里用的是jdk11
2.在pom.xml文件中引入依赖
<dependencies>
<!--引入spring-context依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.18.RELEASE</version>
</dependency>
</dependencies>
3.在路径【src/main/java/com/leo/bean/】下创建实体类Person.java,作为测试的基础类
package com.leo.bean;
public class Person {
private String name;
private Integer age;
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
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 "Person{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
方式一、通过XML配置文件注入JavaBean
1.在路径【src/main/resources】下创建spring配置文件(bean.xml),用于将Person类注入ioc容器
<?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-4.3.xsd">
<!--id:设置一个注入ioc容器的名字,一般默认为类名;class:类的所在位置-->
<bean id="person" class="com.leo.Person">
<!--property:设置属性初始化参数-->
<property name="age" value="24"/>
<property name="name" value="leo"/>
</bean>
</beans>
2.创建测试类(TestMain.java),测试从容器中获取到初始化的javaBean
package com.leo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
//ClassPathXmlApplicationContexty该对象是根据读取配置文件(在类路径下)的方式创建一个ioc容器
ApplicationContext applicationContext_xml = new ClassPathXmlApplicationContext("bean.xml");
//根据配置文件中bean标签的id值获取获取指定的javaBean对象
Person person_xml = (Person) applicationContext_xml.getBean("person");
System.out.println("通过配置文件获取的结果:" + person_xml);
}
}
运行结果
方式二、通过注解方式向ioc容器中注入javaBean
1.在路径【src/main/java/com/leo/config/】创建一个配置类MainConfig,该类的作用就是:通过注解【@Configuration】将该类设置为sping的一个配置类。在配置类中使用注解【@Bean】表示需要在ioc容器中实例化一个javaBean对象。
package com.leo.config;
import com.leo.bean.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration:表示这个类是一个配置类,等同于spring的配置文件(bean.xml)
@Configuration
public class MainConfig {
//@Bean:表示向springioc容器中注册一个javaBean,等同于配置文件中的<bean>标签
//配置文件的id:在配置类中默认为方法名(person),当然也可以通过注解属性value来指定id名 如:@Bean(value = "id名")
@Bean
public Person person() {
//初始化Person对象
return new Person("leo", 24);
}
}
2.测试从ioc容器中获取结果
package com.leo;
import com.leo.bean.Person;
import com.leo.config.MainConfig;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestMain {
public static void main(String[] args) {
//AnnotationConfigApplicationContext该对象是根据读取配置类的方式创建一个ioc容器
ApplicationContext applicationContext_config = new AnnotationConfigApplicationContext(MainConfig.class);
Person person_config = applicationContext_config.getBean(Person.class);
System.out.println("通过配置类获取的结果:" + person_config);
}
}
查看运行结果
细节说明:
1、@Bean是否要跟@Configuration配合使用?
答:如果需要保证该类实例需要注入进springIOC容器那么就需要配合注解@Configuration一起使用。
2、@Bean是否可以单独使用?
答:可以。但是并不会注入到Spring的IOC容器中,相当于有没有注解都一样。跟平常 new 一个对象是一样的。每次调用都会生成一个新的java对象。
3、@Bean和@Configuration一起使用有什么效果?
答:@Bean + @Configuration配合使用时 ,在调用@Bean注解的方法时返回的实例bean是从IOC容器获取的,是已经注入的,且是单例的,而不是新创建的。