Spring控制反转理解和实现
1.pom.xml文件详情
<properties>
<!-- spring版本号 -->
<spring.version>4.2.4.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 1.spring核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 2.spring dao依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 3.spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 4.Spring test依赖:方便做单元测试和集成测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>2.方式一:基于xml方式
(1).UserService接口
public interface UserService {
public void insert();
}(2).UserServiceImpl实现类
public class UserServiceImpl implements UserService{
private String name;
public void setName(String name) {
this.name = name;
}
public void insert() {
// TODO Auto-generated method stub
System.out.println(name+"业务处理成功");
}
}(3).配置application.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- xml文件头添加shema约束 -->
<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">
<!-- 配置,将实现类的对象创建交给Spring管理
bean标签:
id:给bean起个名字,唯一,不能重复,不能出现特殊字符,就看做根据面向接口编程new出来的实例
class:类的全路径,底层根据全路径生成实例
-->
<bean id="userService" class="cn.com.sinodata.service.UserServiceImpl">
<!-- 属性依赖注入,底层会自动封装,只要在dao实现类提供属性和set方法就行 -->
<property name="name" value="张三"></property>
</bean>
</beans>(4).main启动类
public class SpringDemoMain {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
UserService userService = (UserService)applicationContext.getBean("userService");
userService.insert();
}
}3.方式二:.基于注解方式
原理:使用注解方式无需在XML中配置一个Bean引用,使用时更加简单和方便。
第一步:在管理的bean上加上注解@Component (Spring2.5以后引入)
第二步:applicationContext.xml中注解开启和注解扫描(注解开启和注解扫描后,扫描的bean会被自动纳入Spring容器管理)
(1).PeoperService接口
public interface PeoperService {
public void update();
}(2).PeoperServiceImpl实现类
@Component
public class PeoperServiceImpl implements PeoperService{
private String name;
public void setName(String name){
this.name = name;
}
public void update() {
// TODO Auto-generated method stub
System.out.println(name+"信息更新成功");
}
}(3).配置application.xml
<?xml version="1.0" encoding="UTF-8"?>
<!-- xml文件头添加shema约束 引入context名称空间 -->
<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 http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启spring的注解功能,配置扫描包的时候,spring会自动开启注解功能,故此项可不配置-->
<!-- <context:annotation-config/> -->
<!-- 配置bean组件扫描 此时会自动扫描base-package包以及子包下面含有@Component注解的bean,纳入spring容器管理-->
<context:component-scan base-package="cn.com.sinodata.service"/>
</beans>(4).main启动类
public class SpringDemoMain {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
PeoperServiceImpl peoperServiceImpl = (PeoperServiceImpl)applicationContext.getBean("peoperServiceImpl");
peoperServiceImpl.setName("css");
peoperServiceImpl.update();
}
}
文章标题:Spring控制反转理解和实现
发布时间:2020-04-20, 17:31:34
最后更新:2020-04-20, 17:31:34