一、Spring有两种类型bean,
一种普通bean,另外一种工厂bean(FactoryBean)
2、普通bean:在配置文件中定义bean类型就是返回类型
3、工厂bean:在配置文件定义bean类型可以和返回类型不一样 第一步 创建类,让这个类作为工厂bean,实现接口 FactoryBean 第二步 实现接口里面的方法,在实现的方法中定义返回的bean类型
public class Course {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Course{" +
"name='" + name + '\'' +
'}';
}
}
public class MyBean implements FactoryBean<Course> {
//定义返回bean
@Override
public Course getObject() throws Exception {
Course course = new Course();
course.setName("无敌");
return course;
}
@Override
public Class<?> getObjectType() {
return null;
}
@Override
public boolean isSingleton() {
return false;
}
}
<?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="myBean" class="com.cjc.spring.controller.MyBean">
</bean>
</beans>
public class MyBeanTest {
@Test
public void myBeanTest(){
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
//获取配置创建对象
Course course = context.getBean("myBean", Course.class);
//调用方法
System.out.println(course.getName());
}
}
二、bean作用域
1、在Spring里面,设置创建bean实例是单实例还是多实例
2、在Spring里面,默认情况下,bean是单实例对象
@Test
public void arrTest01(){
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
//获取配置创建对象
Book book1 = context.getBean("book", Book.class);
Book book2 = context.getBean("book", Book.class);
//调用方法
System.out.println(book1);
System.out.println(book2);
3、设置单实例还是多实例
(1)在spring配置文件bean标签里面有属性(scope)用于设置单实例还是多实例
(2)scope属性值 第一个值 默认值,singleton,表示是单实例对象
第二个值 prototype,表示是多实例对象
三、bean生命周期
(1)通过构造器创建bean实例(无参数构造)
(2)为bean的属性设置值和对其他bean引用(调用set方法)
(3)把bean实例传递bean后置处理器的方法postProcessBeforeInitialization
(4)调用bean的初始化的方法(需要进行配置初始化的方法)
(5)把bean实例传递bean后置处理器的方法 postProcessAfterInitialization
(6)bean可以使用了(对象获取到了)
(7)当容器关闭时候,调用bean的销毁的方法(需要进行配置销毁的方法)
public class Order {
private String name;
public Order() {
System.out.println("第一步,创建bean的实例");
}
public void setName(String name) {
this.name = name;
System.out.println("第二步,调用属性的值");
}
//创建初始化方法
public void initMethod(){
System.out.println("第三步,调用初始化方法");
}
//创建销毁方法
public void deleteOrders(){
System.out.println("第五步,执行销毁方法");
}
}
public class MyBeanPost implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之前执行的方法");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("在初始化之后执行的方法");
return bean;
}
}
<?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="orders" class="com.cjc.spring.bean.Order" init-method="initMethod" destroy-method="deleteOrders">
<property name="name" value="魔幻手机" ></property>
</bean>
<!--配置后置处理器-->
<bean id="myBeanPost" class="com.cjc.spring.bean.MyBeanPost"></bean>
</beans>
public class Test01 {
@Test
public void ordersTest(){
//加载配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");
//获取配置创建对象
Order order = context.getBean("orders", Order.class);
System.out.println("第四步,创建bean实例对象");
//手动销毁
context.close();
}
}
四、xml自动装配
public class Dept {
@Override
public String toString() {
return "Dept{}";
}
}
public class Emp {
private Dept dept;
public void setDept(Dept dept) {
this.dept = dept;
}
public Dept getDept() {
return dept;
}
@Override
public String toString() {
return "Emp{" +
"dept=" + dept +
'}';
}
}
<?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="emp" class="com.cjc.spring.autowire.Emp" autowire="byName">
</bean>
<bean id="dept" class="com.cjc.spring.autowire.Dept"></bean>
</beans>
public class autTest {
@Test
public void arrTest01(){
//加载配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
//获取配置创建对象
Emp emp = context.getBean("emp", Emp.class);
//调用方法
System.out.println(emp);
}
}
五、外部属性文件
prop.driverClassName=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3307/demo
prop.username=root
prop.password=123456
<?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:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClassName}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>