标签:string date throw 设置 pat 创建 encoding property col
Bean容器初始化
FileSystemXmlApplication context = new FileSystemXmlApplicationContext("F://test//context.xml");
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath*/resources/context.xml");
<?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="InjectionServiceImpl" class="com.jing.spring.bean.InjectionServiceImpl"> <property name="injectionDao" ref="injectionDao"></property> </bean> <bean id="injectionDao" class="com.jing.spring.bean.InjectionDaoImpl"></bean><!--??????不懂 程序中是引用接口 为什么却要把实现类注入?--> </beans>
private InjectionDao injectionDaoss; public void setInjectionDao(InjectionDao injectionDao) { this.injectionDaoss = injectionDao; } public void save(String arg) { System.out.print("InjectionServiceImpl中的arg==="+arg+"\n"); //逻辑处理 arg=arg+":"+this.hashCode(); injectionDaoss.save(arg); }
<?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="InjectionServiceImpl" class="com.jing.spring.bean.InjectionServiceImpl"> <constructor-arg name="injectionDao" ref="injectionDao"></constructor-arg> </bean> <bean id="injectionDao" class="com.jing.spring.bean.InjectionDaoImpl"></bean> </beans>
private InjectionDao injectionDaoss; InjectionServiceImpl(InjectionDao injectionDao){ this.injectionDaoss=injectionDao; } public void save(String arg) { System.out.print("InjectionServiceImpl中的arg==="+arg+"\n"); //逻辑处理 arg=arg+":"+this.hashCode(); injectionDaoss.save(arg); }
Bean的配置项
Bean的作用域
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="springLifeDate" class="com.jing.spring.lifedate.SpringLifeDate" init-method="start" destroy-method="end"></bean> </beans>
package com.jing.spring.lifedate;
public class SpringLifeDate1 {
public void start(){
System.out.println("SpringLifeDate开始执行。。。。");
}
public void excecute(){
System.out.println("SpringLifeDate执行中。。。。");
}
public void end(){
System.out.println("SpringLifeDate执行结束。。。。");
}
}
PS:这种方法针对每个Bean设置的初始化/销毁时执行的方法。在Bean容器初始化时主动执行init-method设置的类中的方法,Bean容器销毁后执行destroy-method设置的方法。如果在XML中进行方法设置,Bean类中创造方法,会报错。
package com.jing.spring.lifedate;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
public class SpringLifeDate2 implements InitializingBean, DisposableBean {
public void afterPropertiesSet() throws Exception {
System.out.println("SpringLifeDate开始执行。。。。");
}
public void excecute(){
System.out.println("SpringLifeDate执行中。。。。");
}
public void destroy() throws Exception {
System.out.println("SpringLifeDate执行结束。。。。");
}
}
PS:这种方法是Bean中实现接口实现Bean初始化/销毁时执行的方法。Bean初始化后执行的方法需要实现InitializingBean接口重写afterPropertiesSet方法,Bean销毁后执行的方法需要实现DisposableBean接口重写destroy方法。
<?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" default-init-method="defaultInit" default-destroy-method="defaultDestroy" > </beans>
package com.jing.spring.lifedate; public class SpringLifeDate3 { public void defaultInit() throws Exception { System.out.println("SpringLifeDate3开始执行。。。。"); } public void excecute(){ System.out.println("SpringLifeDate执行中。。。。"); } public void defaultDestroy() throws Exception { System.out.println("SpringLifeDate3执行结束。。。。"); } }
PS:这种方式生命Bean的初始化/销毁方法,在类中没有相对应得方法,也不会报错。
标签:string date throw 设置 pat 创建 encoding property col
原文地址:https://www.cnblogs.com/jixue/p/9911276.html