1.Spring生成Bean的三种方式
model如下
public interface UserDaoInterface {
public void sayHello();
public void sayinit();
public void saydestory();
}
复制代码
1.1 无参数的构造方式,通过id获取 applicationContext.xml中bean配置如下 <bean id="userdaointerface" class="com.dao.UserDaoImp" init-method="sayinit" destroy-method="saydestory">
获取bean方式如下
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 1.通过id获取bean
UserDaoInterface userDaoInterface = (UserDaoInterface) applicationContext.getBean("userdaointerface");
userDaoInterface.sayHello();
复制代码
1.2 静态工厂实例化 applicationContext.xml中bean配置如下 <bean id="bean2" class="com.utils.Bean2Factory" factory-method="getBean2"/>
获取bean方式如下
UserDaoInterface userDaoInterface = Bean2Factory.getBean2();
userDaoInterface.sayHello()
复制代码
public class Bean2Factory {
public static UserDaoInterface getBean2(){
return new UserDaoImp();
}
}
复制代码
1.3 实例化工厂获取bean applicationContext.xml配置bean如下
<bean id="bean3Factory" class="com.utils.Bean3Factory"></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
复制代码
获取bean方式如下
Bean3Factory bean3Factory = new Bean3Factory();
UserDaoInterface userDaoInterface = bean3Factory.getBean3();
userDaoInterface.sayHello();
复制代码
public class Bean3Factory {
public UserDaoInterface getBean3(){
return new UserDaoImp();
}
}
复制代码
2.Spring分配置文件开发
2.1 创建工厂的时候加载多个配置文件 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml","applicationContext2.xml");
2.2 在一个配置文件中包含另外一个配置文件 <import resource="applicationContext2.xml"></import>
3.web.xml不同版本配置头文件
3.0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
</web-app>
复制代码
3.1
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>
复制代码
WiHongNoteBook