Spring概念
开源的轻量级框架 框架特征:
面向切面编程(aop):扩展功能不是修改源代码实现 控制反转(ioc):把对象的创建不是通过new方式实现,而是交给spring配置创建类对象 spring是一站式框架
web层:springMVC service层:spring的ioc dao层:spring的jdbcTemplate spring版本
hibernate5.x spring4.x struts2
Spring的ioc操作
把对象的创建交给spring管理 ioc操作两部分:
IOC底层原理
ioc底层原理使用技术 xml配置文件 dom4j解决xml 工程设计模式 反射
IOC入门案例
第一步:导入jar包(Beans、core、Context、SpEL) 第二步:创建类,在类里面创建方法
public class User {
public void add () {
System.out .println("add......" );
}
public static void main (String[] args) {
User user = new User();
user.add();
}
}
第三步:创建spring配置文件,配置创建类
spring核心配置文件建议放到src下面,建议名称applicationContext.xml 引入schema约束
<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" >
</beans >
<bean id ="user" class ="com.ioc.User" > </bean >
@Test
public void testUser () {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml" );
User user = (User) context.getBean("user" );
System.out .println(user);
user.add();
}
配置文件提示
spring引入schema约束,把约束文件引入到eclipse 复制约束路径:http://www.springframework.org/schema/beans/spring-beans.xsd
Spring的bean管理
bean实例化方式
在spring里面通过配置文件创建对象 bean实例化三种方式
第一种:使用类的无参数构造器创建(建议) 注意 :类里面没有无参数构造器,会出现异常 第二种:使用静态工厂创建
<bean id ="bean2" class ="com.bean.Bean2Factory" factory-method ="getBean2" > </bean >
public static Bean2 getBean2 () {
return new Bean2();
}
@Test
public void testBean2 () {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml" );
Bean2 bean2 = (Bean2) context.getBean("bean2" );
System.out .println(bean2);
}
<bean id ="bean3Facotry" class ="com.bean.Bean3Factory" > </bean >
<bean id ="bean3" factory-bean ="bean3Facotry" factory-method ="getBean3" > </bean >
public Bean3 getBean3 () {
return new Bean3();
}
@Test
public void testBean2 () {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml" );
Bean3 bean3 = (Bean3) context.getBean("bean3" );
System.out .println(bean3);
}
bean属性
id属性 起名称,id属性值名称任意命名;不能包含特殊符号;根据id值得到配置对象 class属性 创建对象所在类的全路径 name属性 功能和id属性一样,name属性可以包含特殊符号 scope属性
singleton:默认值,单例(对象只创建一次)
<bean id="user" class="com.ioc.User" ></bean>
// 1 加载spring配置文件,根据创建对象
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml" )
// 2 得到配置创建的对象
User user1 = (User) context.getBean ("user" )
User user2 = (User) context.getBean ("user" )
System.out .println (user1)
System.out .println (user2)
// 输出结果:
// com .ioc .User @384 ad17b
com .ioc .User @384 ad17b
<bean id ="user" class ="com.ioc.User" scope ="prototype" > </bean >
// 2 得到配置创建的对象
User user1 = (User) context.getBean ("user" )
User user2 = (User) context.getBean ("user" )
System.out .println (user1)
System.out .println (user2)
// 输出结果:
// com .ioc .User @384 ad17b
com .ioc .User @61862 a7f
request:创建对象,把对象放到request域里面 session:创建对象,把对象放到session域里面 globalSession:创建对象,把对象放到globalSession里面
属性注入
创建对象时候,向类里面属性设置值 属性注入的方式介绍(spring里面没有接口注入)
public class User{
private String name;
public User(String name) {
this .name = name;
}
}
User user = new User("lucy" );
public class User{
private String name;
public User(String name) {
this .name = name;
}
}
User user = new User("lucy" );
public interface Dao {
public void delete (String name);
}
public class DaoImpl implements Dao {
private String name;
public void delete (String name) {
this .name = name;
}
}
spring-有参数构造注入
<bean id ="demo" class ="com.property.PropertyDemo1" >
<constructor-arg name ="username" value ="小王小马" > </constructor-arg >
</bean >
public class PropertyDemo1 {
private String username;
public PropertyDemo1 (String username) {
this .username = username;
}
public void test1 () {
System.out .println("demo1......" + username);
}
}
@Test
public void testUser () {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml" );
PropertyDemo1 demo1 = (PropertyDemo1) context.getBean("demo" );
demo1.test1();
}
spring-set方法注入
<bean id ="book" class ="com.property.Book" >
<property name ="bookName" value ="易筋经" > </property >
</bean >
public class Book {
private String bookName;
public void setBookName (String bookName) {
this .bookName = bookName;
}
public void demobook () {
System.out .println("book......." + bookName);
}
}
@Test
public void testUser () {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml" );
Book book = (Book) context.getBean("book" );
book.demobook();
}
注入对象类型属性
创建service类和dao类 具体实现过程
在service里面把dao作为类型属性 生成dao类型的set方法 配置文件中完成注入
<bean id ="userDao" class ="com.ioc.UserDao" > </bean >
<bean id ="userService" class ="com.ioc.UserService" >
<property name ="userDao" ref ="userDao" > </property >
</bean >
P名称空间注入
<beans xmlns ="http://www.springframework.org/schema/beans"
xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p ="http://www.springframework.org/schema/p"
xsi:schemaLocation ="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >
<bean id ="person" class ="com.property.Person" p:pname ="lucy" > </bean >
注入复杂类型
数组 list集合 map集合 properties类型
private String[] arrs;
private List<String> list;
private Map<String, String> map;
private Properties properties;
<bean id ="person" class ="com.property.Person" >
<property name ="arrs" >
<list >
<value > 小王</value >
<value > 小马</value >
<value > 小宋</value >
</list >
</property >
<property name ="list" >
<list >
<value > 小奥</value >
<value > 小金</value >
<value > 小普</value >
</list >
</property >
<property name ="map" >
<map >
<entry key ="aa" value ="lucy" > </entry >
<entry key ="bb" value ="mary" > </entry >
<entry key ="cc" value ="tom" > </entry >
</map >
</property >
<property name ="properties" >
<props >
<prop key ="driverclass" > com.mysql.jdbc.Driver</prop >
<prop key ="username" > root</prop >
</props >
</property >
</bean >
IOC和DI区别
IOC:控制反转,把对象创建交给spring进行配置 DI:依赖注入,向类里面的属性中设置值 两者之间的关系: 依赖注入不能单独存在,西药在ioc基础之上完成操作
Spring整合web项目原理
加载spring核心配置文件:ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
new对象,功能可以实现,效率低 实现思想:把加载配置文件和创建对象过程,在服务器启动时候完成 实现原理
ServletContext对象 监听器:监听对象何时创建 具体使用:
在服务器启动时候,为每个项目创建一个ServletContext对象 在ServletContext对象创建时候,使用监听器可以具体到ServletContext对象在什么时候创建 使用监听器监听到ServletContext对象创建时候
加载spring配置文件,把配置文件配置对象创建 把创建出来的对象放到ServletContext域对象里面(setAttribute方法) 获取对象时候,到ServletContext域得到(getAttribute方法)