一) Spring 介绍、IOC控制反转思想与DI依赖注入
一。spring介绍
1.IOC反转控制思想(Inversion of Control)与DI依赖注入(Dependency Injection)
2.AOP面向切面的编程思想与动态代理
3.作用:项目的粘合剂,总管,使项目的维护性和扩展性更好
二。spring作为bean的管理容器使用步骤
1.pom.xml加入spring的依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>spring</groupId> <artifactId>spring</artifactId> <version>0.0.1-SNAPSHOT</version> <build> <sourceDirectory>src</sourceDirectory> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.5.1</version> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.13.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.3.13.RELEASE</version> </dependency> </dependencies> </project>
2.在src目录下创建beans.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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id="bt" class="com.bean.BeanTest"></bean> <bean id="stuDAO" class="com.dao.StudentDAO"></bean> <bean id="stuMysqlDAO" class="com.dao.StudentMysqlDAO"></bean> <bean id="stuSer" class="com.service.StudentService"> <property name="stuDAO"> <ref bean="stuMysqlDAO" /> </property> </bean> <bean id="stuControl" class="com.control.StudentControl"> <property name="stuSer"> <ref bean="stuSer" /> </property> </bean>
</beans>
3.将java对象加入到配置文件中注册
<bean id="hello" class="com.bean.Hello"></bean>
4.在代码中获取容器,拿到bean对象,运行
// 初始化spring的容器
BeanFactory bf = new ClassPathXmlApplicationContext("beans.xml");
// 从容器中拿到javabean
Hello h = (Hello) bf.getBean("hello");
三。ioc反转控制。依赖注入
1.set方法的注入
2.构造方法的注入
四。注解的方式完成注入
1.补充schema
2.打开注解开关
<!-- 打开spring的注解开关 -->
<context:annotation-config></context:annotation-config>
<!-- 告诉spring到哪些包下去扫描bean对象 -->
<context:component-scan base-package="com"></context:component-scan>
3.给类加注解
@comptent:表示注册为组件
@Repository:表示注册为DAO组件
@Service:表示注册为业务组件
@Controller:注册为控制器组件
4.注入的注解
@Autowired:写在属性上,表示自动按照类型注入
注意:
a。属性的类型必须是接口
b。如果一个接口有多个实现类, 通过@Qualifier("db2")指定实现类
附:spring注解可参考此链接 https://www.cnblogs.com/wlxslsb/p/10718402.html
实例:使用注解的形式来写一个Spring项目
1.dao层
IStudentDAO.java
package com.dao; public interface IStudentDAO { public void saveStu(); }
StudentDAO.java
package com.dao; import org.springframework.stereotype.Component; import org.springframework.stereotype.Repository; //@Component("oracle") @Repository("oracle") public class StudentDAO implements IStudentDAO{ @Override public void saveStu() { System.out.println("使用oracle数据库"); System.out.println("正在保存学员对象"); } }
StudentMysqlDAO.java
package com.dao; import org.springframework.stereotype.Component; @Component("mysql") public class StudentMysqlDAO implements IStudentDAO{ @Override public void saveStu() { System.out.println("正在使用mysql数据库"); System.out.println("保存学员对象"); } }
2.Service层
IStudentService.java
package com.service; public interface IStudentService { public void addStudent(); }
StudentService.java
package com.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import com.dao.IStudentDAO; import com.dao.StudentDAO; //@Component @Service public class StudentService implements IStudentService{ // IStudentDAO stuDAO = new StudentDAO(); @Autowired @Qualifier("mysql")//当借口有多个实现类时,必须指明注入哪个实现类 IStudentDAO stuDAO; public void setStuDAO(IStudentDAO stuDAO) { this.stuDAO = stuDAO; } @Override public void addStudent() { System.out.println("拿到数据"); System.out.println("调用dao保存数据"); stuDAO.saveStu(); } }
3.Control层
StudentControl.java
package com.control; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.stereotype.Controller; import com.service.IStudentService; //@Component("sc") @Controller public class StudentControl { @Autowired IStudentService stuSer; public void setStuSer(IStudentService stuSer) { this.stuSer = stuSer; } public String execute(){ System.out.println("接收页面参数"); System.out.println("调用业务层"); stuSer.addStudent(); return "success"; } }
4.配置文件
beans.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" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 打开spring的注解功能 --> <context:annotation-config></context:annotation-config> <!-- 告诉spring到哪些包下去扫描bean对象 --> <context:component-scan base-package="com"></context:component-scan> </beans>
5.测试代码
RunTest.java
package com.bean; import org.springframework.beans.factory.BeanFactory; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.control.StudentControl; import com.service.IStudentService; public class RunTest { public static void main(String[] args) { //根据beans.xml去构造出bean工厂,就是spring的容器 BeanFactory bf = new ClassPathXmlApplicationContext("beans.xml"); StudentControl sc = (StudentControl) bf.getBean("sc"); sc.execute(); } }