一.Bean的实例化
Bean的实例化即在Spring容器中创建对象的过程。
1.构造方法实例化(最常用)
(1)创建实体类
@Data
public class Student {
private String name;
private int age;
private double salary;
}
(2)配置文件
public class SpringTest {
public static void main(String[] args) {
ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)app.getBean("student");
System.out.println(student);
}
}
(3)测试类
public class SpringTest {
public static void main(String[] args) {
ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)app.getBean("student");
System.out.println(student);
}
}
2.静态工厂实例化
(1)创建工厂类
/**
* 告诉spring容器调用createInstance()方法,
* 并将返回值存到spring容器中
*/
public class StudentStaticFactory {
public static Student createInstance() {
return new Student();
}
}
(2)配置文件
- id:容器中对象的唯一标识
- class:工厂类的全限定类名
- factory-method:工厂类里的静态方法
<bean id="studentFactory" class="com.spring.bean.StudentStaticFactory" factory-method="createInstance"></bean>
(3)测试类
public class SpringTest {
public static void main(String[] args) {
ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)app.getBean("studentFactory");
System.out.println(student);
}
}
3.实例工厂实例化
(1)创建工厂类
public class StudentInstanceFactory {
public Student getInstance() {
return new Student();
}
}
(2)配置文件
- factory-bean:指定配置工厂,是工厂实例在Spring中的唯一标识
- factory-method:工厂类中实例化bean的方法
<!-- 配置工厂,创建工厂类对象 -->
<bean id="factory" class="com.spring.bean.StudentInstanceFactory"></bean>
<bean id="studentInstanceFactory" factory-bean="factory" factory-method="getInstance"></bean>
(3)测试类
public class SpringTest {
public static void main(String[] args) {
ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)app.getBean("studentInstanceFactory");
System.out.println(student);
}
}
二、Bean的装配方式
概念:Bean的装配即Bean依赖注入到Spring容器中,就是依赖注入的方式
1.基于XML配置的装配
构造方法注入和使用set方法注入。
2.基于注解的装配
使用注解需要导入Spring AOP的jar包
@Component
作用:该注解加在类上,当Spring容器扫描到该注解就会为该类创建对象。即相当于在xml中配置一个bean。
- 若注解为@Component:创建的对象id为类名首字母小写
- 若注解为@Component(“stu”):创建的对象id值为stu
@Controller
作用:功能与Component一样,用于表现层(Web层)
@Service
作用:功能与Component一样,用于业务层(Service层)
@Repository
作用:功能与Component一样,用于数据访问层(Dao层)
以上四个注解只能用于标注一个类
@Value
作用:用于给属性赋值
@Autowired
作用:根据属性的类型,自动注入,只能注入bean类型。可对属性、方法和构造方法标注。
@Qualifier
作用:在Autowired注入的基础上,根据名字(Bean的id)注入,需要和Autowired一起使用,不能单独使用
@Resource
作用:根据Bean的id注入,相当于@Autowired+@Qualifier的作用。只能注入bean类型
实例:
1.创建bean的实现类
@Data
@Component
public class Student {
@Value("001")
private int id;
@Value("张三")
private String name;
@Value("男")
private String gender;
@Resource(name="wf")
private Wife wife;
@Autowired
private Date date;
}
@Data
@Component("wf")
public class Wife {
@Value("lili")
private String name;
@Value("25")
private int age;
}
2.配置文件
- context:表示去扫描包和该包的子包
- base-package:包名
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.spring.bean"></context:component-scan>
<bean id="now" class="java.util.Date"></bean>
</beans>
3.测试Bean实例
public class SpringTest {
public static void main(String[] args) {
ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
Student student = (Student)app.getBean("student");
System.out.println(student);
}
}
运行结果:
3.全注解方式
@Configuration
作用:加该注解的类是配置类。一个配置类相当于一个配置文件。创建容器时从该类上加载注解
@ComponentScan
作用:指定该配置类要扫描的包。为src下总包的名称
@Bean
作用:相当于bean标签。只能加在方法上,表示用该方法创建一个对象,然后放入Spring容器中。返回值为创建的对象,对象的id为方法名。
1.创建实现类
@Data
@Component("tea")
public class Teacher {
@Value("WangLei")
private String name;
@Value("27")
private int age;
@Value("3000")
private double salary;
@Autowired
private Date date;
}
2.创建配置类
@Configuration
@ComponentScan("com.spring")
public class Config {
@Bean
public Date date() {
return new Date();
}
}
3.测试
public class SpringTest {
public static void main(String[] args) {
ApplicationContext app = new AnnotationConfigApplicationContext(Config.class);
Teacher teacher = (Teacher)app.getBean(Teacher.class);
System.out.println(teacher);
}
}