1.Spring定义
轻量级框架, Java EE的春天,当前主流框架
2.Spring特性
1.IOC (控制反转,DI:依赖注入)
2.AOP实现 (面向切面编程)
3.Spring理念
面向Bean的编程
4.Spring优势
低侵入式设计
独立于各种应用服务器
依赖注入特性将组件关系透明化,降低了耦合度
面向切面编程特性允许将通用任务进行集中式处理
与第三方框架的良好整合
5.IOC特性
A.非IOC思想
(1)创建maven工程,java,resources,test,并配置路径参数
(2)UserService
public interface UserService {
public void print();
}
(3)UserServiceImpl
public class UserServiceImpl implements UserService{
@Override
public void print() {
System.out.println("输出****HelloWorld*********");
}
}
(4)Test001
@Test
public void testPrint(){
UserServiceImpl userService = new UserServiceImpl();
userService.print();
}
B.IOC思想
(1)pom
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
(2)spring配置文件applicationContext.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:对象的自定义名称,唯一值,spring通过这个名称找到对象-->
<!--class:类的全路径名称-->
<bean id="userService" class="cn.kgc.service.UserServiceImpl"></bean>
</beans>
(3)Test001
@Test
public void testSpringPrint(){
String conf = "beans.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
UserService userService = (UserService) ac.getBean("userService");
//测试
userService.print();
}
6.DI注入(IOC的实现)
(1)set注入
1.1-属性赋值
A.实体类User
package cn.kgc.entity;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setId(Integer id) {
System.out.println("调用方法setId......");
this.id = id;
}
public void setName(String name) {
System.out.println("调用方法setName......");
this.name = name;
}
}
B.applicationContext.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="user" class="cn.kgc.entity.User">
<property name="id" value="11"/> ---->相当于启用命令调用setId()方法
<property name="name" value="zs"/> ---->相当于启用命令调用setName()方法
</bean>
</beans>
C.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
//测试
System.out.println("编码:"+user.getId()+"名称:"+user.getName());
}
1.2引用类型手工注入(ref方式)
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User">
<property name="id" value="11"/>
<property name="name" value="zs"/>
<property name="role" ref="myRole"/>
</bean>
<bean id="myRole" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
D.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("user名称:"+user.getRole().getrName());
}
1.3引用类型自动注入
(1)byName
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
/**
* byName(按名称注入):
* java类中引用类型的属性名和spring容器中(配置文件)<bean>的id名称一样,且
* 数据类型是一致的,这样的容器中的bean,spring能够赋值给引用类型
*/
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User" autowire="byName">
<property name="id" value="11"/>
<property name="name" value="zs"/>
<!--<property name="role" ref="myRole"/>-->
</bean>
<bean id="role" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
D.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("Role名称:"+user.getRole().getrName());
}
(2)byType
第一种同源(一致性)
A.User
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class User {
private Integer id;
private String name;
private Role role;
/**
*byType(按类型注入):java类中引用类型的数据类型和spring容器中(配置文件)<bean>的class属性
* 是同源关系的,这样的bean能够赋值给引用类型
*
* 同源就是一类型的意思:
* 例如:第一种同源.java引用类型的数据类型和bean的class的值是一样的
* 第二种同源.java引用类型的数据类型和bean的class的值是父子类间的关系
* 第三种同源.java引用类型的数据类型和bean的class的值是接口和实现类的关系
*/
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
B.Role
package cn.kgc.entity;
import org.springframework.stereotype.Component;
public class Role {
private Integer rid;
private String rName;
public Integer getRid() {
return rid;
}
public void setRid(Integer rid) {
this.rid = rid;
}
public String getrName() {
return rName;
}
public void setrName(String rName) {
this.rName = rName;
}
}
C.applicationContext.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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!--声明对象-->
<bean id="user" class="cn.kgc.entity.User" autowire="byType">
<property name="id" value="33333"/>
<property name="name" value="555555"/>
</bean>
<bean id="role3" class="cn.kgc.entity.Role">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
</beans>
D.Test001
@Test
public void testSpringPrintUser(){
String conf = "applicationContext.xml";
//创建Spring容器对象(ClassPathXmlApplicationContext:加载配置文件)
ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
//从容器中获取对象
User user = (User) ac.getBean("user");
System.out.println("Role名称:"+user.getRole().getrName());
}