一.什么是Spring?
Spring是一个开源的轻量级Java开发框架。是一种简化应用程序的开发框架。
在Spring出来之前,service层调用dao层都是用new的方式,在Spring出来之后,service层和dao层都会放在Spring容器去管理,这是Spring的第一种特性,我们称之为IOC,控制反转。
Spring还有一种特性,我们称之为AOP,也就是所谓的"面向切面",说白了就是专门的人干专门的事。
在项目很多公有的或是要被重复调用的模块可以被抽取出来,利用的就是AOP的特性,例如日志模块。
二.Spring特性
特性1.IOC(控制反转),DI(IOC的实现,依赖注入)
那么有哪些注入方式?
(1)set注入(常用)
(2)注解注入(常用)
常用的就是以上两种,当然不止这三种注入方式,还有其他几种注入方式,只不过运用极少。
(3)构造器注入等…
特性2.AOP实现(面向切面编程)
三.Spring中的核心思想/理念是什么?
面向Bean编程(在Spring中万物都是Bean组件,面向Bean就是创建new)
四.Spring优势
1.低侵入式设计
2.独立于各种应用服务器
3.依赖注入特性将组件关系透明化,降低耦合度
4.面向切面编程特性允许将通用任务进行集中式处理
5.与第三方框架的良好整合
五.IOC特性
1.非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)Test
@Test
public void testPrint(){
UserServiceImpl userService = new UserServiceImpl();
userService.print();
}
2.IOC思想
(1)pom.xml
哪个版本都可以,目前没发现哪个版本有问题
<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对象,因为要调用UserServiceImpl的方法,id填写接口的名字-->
<!--byName的意义就是通过property标签中的名称name,name="userDao"的userDao映射到实现类的userDao属性。
ref="userDao"当中的userDao映射到bean id="userDao"的userDao-->
<bean id="userService" class="cn.kgc.service.UserServiceImpl" autowire="byName">
<property name="userDao" ref="userDao"></property>
</bean>
<!--创建bean对象,因为要调用UserDaoImpl的方法-->
<bean id="userDao" class="cn.kgc.dao.UserDaoImpl"></bean>
</beans>
(3)Test
package cn.kgc.test;
import cn.kgc.service.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUserService {
@Test
public void testAdd(){
//连接Spring的配置文件
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//获取Spring的配置文件中需要调用的对象id
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
}
六.DI注入(IOC实现)
1.set注入
(1)属性赋值
实体类User
package cn.kgc.entity;
public class User {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
System.out.println("调用setName方法");
this.name = name;
}
public String getPwd() {
System.out.println("调用setPwd方法");
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
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">
<!--class文件填写的是需要调用的方法的实现类的地址-->
<!-- id一般是写接口名,首字母小写-->
<!-- 这里的bean相当于new UserService-->
<!-- byName-->
<bean id="user" class="cn.kgc.entity.User" autowire="byName">
<!-- name就是user里面的属性名称-->
<!-- ref就是找当前整个配置文件当中其他id为XXX的bean-->
<property name="name" value="zs"></property>
<property name="pwd" value="123"></property>
</bean>
</beans>
Test
package cn.kgc.test;
import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
@Test
public void add(){
//1.加载Spring配置文件路径,括号里填写Spring配置文件名。也就是获取applicationContext.xml整个配置文件
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
//2.上一步获得了整个配置文件,现在要获取bean,通过ac.getBean()方法获取id为user的bean
User user = (User) ca.getBean("user");
//3.获取到bean之后调用它的方法
System.out.println("名字:"+user.getName()+" 密码:"+user.getPwd());
}
}
2.引用类型手工注入(ref方式)
Person注入Menu
Person实体类
package cn.kgc.entity;
public class Person {
private Integer id;
private String name;
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;
}
}
Menu实体类
package cn.kgc.entity;
public class Menu {
private Integer mId;
private String mName;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Integer getmId() {
return mId;
}
public void setmId(Integer mId) {
this.mId = mId;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
}
配置文件applicationContext.xml
<bean id="person" class="cn.kgc.entity.Person">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="menu" class="cn.kgc.entity.Menu">
<property name="mId" value="1"></property>
<property name="mName" value="牛肉"></property>
<property name="person" ref="person"></property>
</bean>
测试类
@Test
public void testMenu(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
Menu menu = (Menu) ca.getBean("menu");
System.out.println(menu.getPerson().getName());
}
3.引用类型自动注入
Java类中引用类型的属性名和spring容器中(配置文件)的id名称一样,且数据类型是一致的,这样,容器中的bean,通过spring就能够赋值给引用类型。
(1)byName 按名称注入
Person
package cn.kgc.entity;
public class Person {
private Integer id;
private String name;
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;
}
}
Menu
package cn.kgc.entity;
public class Menu {
private Integer mId;
private String mName;
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public Integer getmId() {
return mId;
}
public void setmId(Integer mId) {
this.mId = mId;
}
public String getmName() {
return mName;
}
public void setmName(String mName) {
this.mName = mName;
}
}
applicationContext.xml
<bean id="person" class="cn.kgc.entity.Person">
<property name="id" value="1"></property>
<property name="name" value="张三"></property>
</bean>
<bean id="menu" class="cn.kgc.entity.Menu" autowire="byName">
<property name="mId" value="1"></property>
<property name="mName" value="牛肉"></property>
</bean>
测试类
@Test
public void testMenu(){
ClassPathXmlApplicationContext ca = new ClassPathXmlApplicationContext("applicationContext.xml");
Menu menu = (Menu) ca.getBean("menu");
System.out.println(menu.getPerson().getName());
}
(2)byType 按类型注入
Java类中引用类型的数据类型和spring容器中(配置文件)的class属性是同源关系,这样的bean能够赋值给引用类型。
同源就是同一类型的意思:
第一种同源:java引用类型的数据类型和bean的class的值一样
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;
}
}
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;
}
}
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>
测试类
@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());
}
第二种同源:java引用类型的数据类型和bean的class的值是父子类间的关系
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;
}
}
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;
}
}
SonRole
package cn.kgc.entity;
public class SonRole extends Role{
}
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="sonRole" class="cn.kgc.entity.SonRole">
<property name="rid" value="666"/>
<property name="rName" value="777"/>
</bean>
测试类
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());
}
第三种同源:java引用类型的数据类型和bean的class的值是接口和实现类的关系
mapper/UserMapper.java
public interface UserMapper {
public void add();
}
mapper/UserMapperImpl.java
public class UserMapperImpl implements UserMapper{
@Override
public void add() {
System.out.println("add UserMapper....");
}
}
service/UserService.java
public interface UserService {
public void add();
}
service/UserServiceImpl
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
@Override
public void add() {
userMapper.add();
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
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="userService" class="cn.kgc.service.UserServiceImpl" autowire="byType">
</bean>
<bean id="userMapper" class="cn.kgc.mapper.UserMapperImpl"></bean>
</beans>
测试类
@Test
public void testprint3(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
拓展(第三种同源换成byName方式实现)
mapper/UserMapper.java
package cn.kgc.mapper;
public interface UserMapper {
public void add();
}
mapper/UserMapperImpl.java
package cn.kgc.mapper;
public class UserMapperImpl implements UserMapper{
@Override
public void add() {
System.out.println("add UserMapper....");
}
}
service/UserService
public interface UserService {
public void add();
}
service/UserServiceImpl
package cn.kgc.service;
import cn.kgc.mapper.UserMapper;
public class UserServiceImpl implements UserService{
private UserMapper userMapper;
@Override
public void add() {
userMapper.add();
}
public UserMapper getUserMapper() {
return userMapper;
}
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
}
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="userService" class="cn.kgc.service.UserServiceImpl" autowire="byName">
<property name="userMapper" ref="userMapper"/>
</bean>
<bean id="userMapper" class="cn.kgc.mapper.UserMapperImpl">
</bean>
</beans>
测试类
public class TestUserServiceImpl {
@Test
public void testprint3(){
String conf = "applicationContext.xml";
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
UserService userService = (UserService) ac.getBean("userService");
userService.add();
}
}
本文介绍了Spring的核心特性,包括IOC(控制反转)和DI(依赖注入),详细阐述了set注入、注解注入、构造器注入等多种DI方式,并探讨了AOP在Spring中的实现。同时,解释了Spring中的非IOC思想与IOC思想,以及如何通过配置文件和注解实现Bean的管理。
653

被折叠的 条评论
为什么被折叠?



