pom.xml
<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>com.monkey1024</groupId>
<artifactId>02_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.4.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>02_spring</finalName>
<plugins>
<!-- 编译插件,指定编译用的的jdk版本 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<!-- jdk的版本号 -->
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 使用构造方法进行注入 -->
<bean id="userService" class="com.monkey1024.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDaoId"/>
</bean>
<bean id="userDaoId" class="com.monkey1024.dao.impl.UserDaoImpl"/>
<!-- 使用set方式进行注入 -->
<!-- <bean id="userService" class="com.monkey1024.service.impl.UserServiceImpl">
<property name="userDao" ref="userDaoId"/>
</bean>
<bean id="userDaoId" class="com.monkey1024.dao.impl.UserDaoImpl"/> -->
</beans>
UserDao.java
package com.monkey1024.dao;
public interface UserDao {
void addUser();
}
UserDaoImpl.java
package com.monkey1024.dao.impl;
import com.monkey1024.dao.UserDao;
public class UserDaoImpl implements UserDao{
public void addUser(){
System.out.println("添加用户");
}
}
UserService.java
package com.monkey1024.service;
public interface UserService {
void addUser();
}
UserServiceImpl.java
package com.monkey1024.service.impl;
import com.monkey1024.dao.UserDao;
import com.monkey1024.service.UserService;
public class UserServiceImpl implements UserService {
private UserDao userDao;
//通过set方法进行注入
// public UserDao getUserDao(){
// return userDao;
// }
// public void setUserDao(UserDao userDao){
// this.userDao = userDao;
// }
//通过构造方法进行注入
public UserServiceImpl(UserDao userDao){
this.userDao = userDao;
}
public void addUser(){
//以前需要手动创建对象
//userDao = new UserDaoImpl();
userDao.addUser();
}
}
Test01.java
package com.monkey1024.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.monkey1024.service.UserService;
public class Test01 {
@Test
public void testDI(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) context.getBean("userService");
userService.addUser();
}
}