SpringMVC项目, Spring容器注入bean的操作方法:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
一般常用第二种,通过xml 配置通常要使用下面几个属性:
init-method: 初始化执行
destroy-method: 销毁时执行
constructor-arg :设置bean构造器,第0个参数的值
property: 设置bean类属性的值
项目具体配置方法如下
web.xml配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-service.xml
</param-value>
</context-param>
applicationContext-service.xml配置:
<bean id="httpClientManager" class="com.jdj.common.utils.HttpClientManager"
init-method="closeIdleStart" destroy-method="destory">
<constructor-arg index="0" name="connectionTimeOut"
value="5000" />
<constructor-arg index="1" name="soTimeOut" value="500000" />
<property name="maxTotal" value="1023" />
</bean>
SpringMVC注入bean操作
init-method: 初始化执行
destroy-method: 销毁时执行
constructor-arg :设置bean构造器,第0个参数的值
property: 设置bean类属性的值
bean调用
public static void main(String[] args) {
AbstractApplicationContext context =new ClassPathXmlApplicationContext("SpringBeans.xml");
PersonService person = (PersonService)context.getBean("personService");
person.setMessage("hello spring");
PersonService person_new = (PersonService)context.getBean("personService");
System.out.println(person.getMessage());
System.out.println(person_new.getMessage());
context.registerShutdownHook();
}
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
第三种是: 通过bean实现InitializingBean和 DisposableBean接口
一般常用第二种,通过xml 配置通常要使用下面几个属性:
init-method: 初始化执行
destroy-method: 销毁时执行
constructor-arg :设置bean构造器,第0个参数的值
property: 设置bean类属性的值
项目具体配置方法如下
web.xml配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext-service.xml
</param-value>
</context-param>
applicationContext-service.xml配置:
<bean id="httpClientManager" class="com.jdj.common.utils.HttpClientManager"
init-method="closeIdleStart" destroy-method="destory">
<constructor-arg index="0" name="connectionTimeOut"
value="5000" />
<constructor-arg index="1" name="soTimeOut" value="500000" />
<property name="maxTotal" value="1023" />
</bean>
SpringMVC注入bean操作
init-method: 初始化执行
destroy-method: 销毁时执行
constructor-arg :设置bean构造器,第0个参数的值
property: 设置bean类属性的值
bean调用
public static void main(String[] args) {
AbstractApplicationContext context =new ClassPathXmlApplicationContext("SpringBeans.xml");
PersonService person = (PersonService)context.getBean("personService");
person.setMessage("hello spring");
PersonService person_new = (PersonService)context.getBean("personService");
System.out.println(person.getMessage());
System.out.println(person_new.getMessage());
context.registerShutdownHook();
}