Spring
理念:使现有技术更加实用,本身是大杂烩整合现有的框架技术
优点:
轻量级框架
Ioc 容器-控制反转 inversion of Control
Aop 面向切面编程
对事务支持
对框架的支持
一、Ioc 控制反转
Ioc 是一种编程思想,由主动编程变为被动接收
别名:依赖注入 dependency injection
控制:
指谁来控制对象的创建
传统的应用程序对象的创建是由程序本身控制的
使用 spring 之后,由 spring 创建对象
反转:
正转指程序来创建对象
反转指程序本身不创建对象,而变成被动接受对象
总结:
以前对象是由程序本身创建,
使用 spring 之后,程序变为接收 spring 创建好的对象
简单示例
1、依赖 pom.xml
org.springframework spring-context-support 5.2.6.RELEASE org.springframework spring-context 5.2.6.RELEASE
2、Person.java
package com.pengshiyu.bean; public class Person { private String name; public void setName(String name) { this.name = name; } public void sayHello() { System.out.println("hello " + this.name); } }
3、beans.xml
此处是完整写法,之后将采用简写形式
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">
4、Demo.java
package com.pengshiyu.spring; import com.pengshiyu.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { public static void main(String[] args) { // 解析beans.xml 文件,生成对应的Bean对象 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person)context.getBean("person"); person.sayHello(); } }
二、Dao 管理示例
Ioc: 对象由 spring 来创建
1、UserDao
package com.spring.dao; public interface UserDao { public void getUser(); }
2、UserDaoMysqlImpl
package com.spring.dao.impl; import com.spring.dao.UserDao; public class UserDaoMysqlImpl implements UserDao { @Override public void getUser() { System.out.println("Mysql 获取用户信息"); } }
3、UserDaoOracleImpl
package com.spring.dao.impl; import com.spring.dao.UserDao; public class UserDaoOracleImpl implements UserDao { @Override public void getUser() { System.out.println("Oracle 获取用户信息"); } }
4、UserService
package com.spring.service; public interface UserService { public void getUser(); }
5、UserServiceImpl
package com.spring.service.impl; import com.spring.dao.UserDao; import com.spring.service.UserService; public class UserServiceImpl implements UserService { private UserDao userDao = null; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void getUser() { this.userDao.getUser(); } }
6、beans.xml(简化版)
7、TestDemo
package com.spring.test; import com.spring.service.UserService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestDemo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); UserService service = (UserService)context.getBean("service"); service.getUser(); } }
三、使用 Ioc 来创建对象的 3 种方法
Person 类
package com.pengshiyu.bean; public class Person { private String name; public Person() { } public Person(String name) { this.name = name; } public void sayHello() { System.out.println("hello " + this.name); } }
创建对象
package com.spring.test; import com.pengshiyu.bean.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Demo { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); Person person = (Person) context.getBean("person"); person.sayHello(); } }
beans.xml(简化版)
1、无参构造
<beans> <bean name="person" class="com.pengshiyu.bean.Person" /> </beans>
2、有参构造
(1)根据参数下标设置
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <!-- index 构造方法下标从 0 开始 --> <constructor-arg index="0" value="Tom" /> </bean> </beans>
(2)根据参数名称设置
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <!-- name 参数名--> <constructor-arg name="name" value="Tom" /> </bean> </beans>
(3)根据参数类型设置
<beans> <bean name="person" class="com.pengshiyu.bean.Person"> <!-- type 参数类型 --> <constructor-arg type="java.lang.String" value="Tom" /> </bean> </beans>