常用依赖注入的方式
- setter注入
<bean id="person" class="cn.spy.spring.test.Person">
<property name="name" value="张三"></property>
<property name="age" value="22"></property>
</bean>
- 构造器注入
<bean id="person" class="cn.spy.spring.test.Person">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
自动装配
首先spring在默认情况下是不会为Bean进行装配的,需要通过autowire来进行。
- byType:IOC容器使用与Bean类型相匹配的属性类进行注入
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceImpl" autowire="byType"></beans:bean>
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl"></beans:bean>
public class MyServiceImpl {
private MyDAOImpl myDAOImpl;
private YourDAOImpl yourDAOImpl;
public void setMyDAOImpl(MyDAOImpl myDAOImpl) {
this.myDAOImpl = myDAOImpl;
}
public void setYourDAOImpl(YourDAOImpl yourDAOImpl) {
this.yourDAOImpl = yourDAOImpl;
}
public void print() {
myDAOImpl.insert();
yourDAOImpl.insert();
}
}
public class MyDAOImpl {
public void insert() {
System.out.println("插入我的记录");
}
}
public class YourDAOImpl {
public void insert() {
System.out.println("插入你的记录");
}
}
测试:
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
MyServiceImpl myServiceImpl = context.getBean("myServiceImpl", MyServiceImpl.class);
myServiceImpl.print();
}
Bean过滤的配置:autowire-candidate
下面例子中,过滤掉了yourDAOImpl这个Bean的注入,那么自然出现空指针异常。
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceImpl" autowire="byType"></beans:bean>
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl" autowire-candidate="false"></beans:bean>
结果:
插入我的记录
Exception in thread "main" java.lang.NullPointerException
at cn.spy.service.MyServiceImpl.print(MyServiceImpl.java:22)
at cn.spy.spring.source.test.MySpringDI.main(MySpringDI.java:13)
- byName:IOC容器使用与Bean名称相匹配的属性类注入。
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceImpl" autowire="byName"></beans:bean>
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl" autowire-candidate="false"></beans: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"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.0.xsd"
default-autowire="byName"></beans>
静态注入
- 工厂类使用静态方法创建对象
<beans:bean id="myServiceImpl" class="cn.spy.service.MyServiceFactory"
factory-method="getStaticInstance" autowire="byType">
</beans:bean>
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl"></beans:bean>
工厂类静态方法:
public class MyServiceFactory {
public static MyServiceImpl getStaticInstance() {
return new MyServiceImpl();
}
}
- 工厂类使用非静态方法创建对象
<beans:bean id="myServiceFactory" class="cn.spy.service.MyServiceFactory"></beans:bean>
<beans:bean id="myServiceImpl" factory-bean="myServiceFactory"
factory-method="getInstance" autowire="byType">
</beans:bean>
<beans:bean id="myDAOImpl" class="cn.spy.dao.MyDAOImpl"></beans:bean>
<beans:bean id="yourDAOImpl" class="cn.spy.dao.YourDAOImpl"></beans:bean>
工厂类非静态方法:
public class MyServiceFactory {
public MyServiceImpl getInstance() {
return new MyServiceImpl();
}
}
Bean作用域
spring默认的作用域是Singleton单例模式。
测试程序:
public static void main(String[] args) {
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
Person person1 = ac.getBean("person", Person.class);
Person person2 = ac.getBean("person", Person.class);
System.out.println(person1 == person2);
}
- singleton:Bean仅创建一个实例,默认作用域。
<bean id="person" class="cn.spy.spring.test.Person" scope="singleton">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
- prototype:每一次对Bean定义或者其他获取都创建一个新的实例。
<bean id="person" class="cn.spy.spring.test.Person" scope="prototype">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
- request:在web请求过程中使用相同Bean实例,每一个web请求都创建一个新的Bean实例。
<bean id="person" class="cn.spy.spring.test.Person" scope="request">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
- session:在不同的Http会话中创建一个新的实例。仅适用支持web的ApplicationContrexts。
<bean id="person" class="cn.spy.spring.test.Person" scope="session">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
- globalSession:仅适用于基于portlet的web应用程序的上下文。
<bean id="person" class="cn.spy.spring.test.Person" scope="globalSession">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
延迟初始化
延迟初始化就是在spring ioc容器启动时不进行加载初始化,在使用的时候才开始加载初始化。
<bean id="person" class="cn.spy.spring.test.Person" lazy-init="true">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"
default-lazy-init="true"></beans>
Bean生命周期
<bean id="person" class="cn.spy.spring.test.Person" init-method="initM" destroy-method="destoryM">
<constructor-arg name="name" value="张三"></constructor-arg>
<constructor-arg name="age" value="22"></constructor-arg>
</bean>
public class Person {
private String name;
private Integer age;
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public void initM() {
System.out.println("Bean初始化执行的方法");
}
public void destoryM() {
System.out.println("Bean销毁执行的方法");
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
说明:init-method字段在Bean创建完毕之后,初始化方法会被spring IOC容器调用。而对于destory-method,如果singleton作用域则在spring IOC容器关闭时调用;如果request作用域则在当前web请求结束之后调用。但是prototype作用则无法跟踪Bean实例化之后的销毁,所以destory-method不会被调用。
网友评论