IoC入门案例制作步骤
案例目录结构

1.pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<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</groupId>
<artifactId>java-01</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
2.编写业务层与表现层(模拟)接口与实现类
package com.limei.service;
public interface UserService {
public void save();
}
package com.limei.service.impl;
import com.limei.dao.UserDao;
import com.limei.service.UserService;
public class UserServiceImpl implements UserService {
public UserServiceImpl() {
}
private UserDao userDao;
private int num;
public void setNum(int num) {
this.num = num;
}
//1.对需要进行注入的变量加set方法
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void save() {
System.out.println("user service running..."+num);
userDao.save();
}
}
3.编写持久层层接口与实现类
package com.limei.dao;
public interface UserDao {
public void save();
}
package com.limei.dao.impl;
import com.limei.dao.UserDao;
public class UserDaoImpl implements UserDao{
private String userName;
private String password;
private int num;
private String name;
public UserDaoImpl() {
}
public void setNum(int num) {
this.num = num;
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setPassword(String password) {
this.password = password;
}
public void setName(String name) {
this.name = name;
}
public void save(){
System.out.println("user dao running..."+userName+" "+password+" "+num
);
}
}
4.建立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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1.加载context命名空间的支持-->
<!--xmlns:context="http://www.springframework.org/schema/context"-->
<!--2.加载配置文件-->
<context:property-placeholder location="classpath:data.properties" />
<!--2.将要注入的资源声明为bean-->
<bean id="userDao" class="com.limei.dao.impl.UserDaoImpl" >
<property name="userName" value="${username1}"/>
<property name="password" value="${pwd}"/>
<!--<property name="name" value="${name1}"/>-->
<property name="num" value="99999999"></property>
</bean>
<!--<bean id="bookDao" class="com.limei.dao.impl.BookDaoImpl"/>-->
<bean id="userService" class="com.limei.service.impl.UserServiceImpl">
<!--3.ref为引用类型,name为UserServiceImpl里的setUserDao-->
<property name="userDao" ref="userDao"/>
<property name="num" value="1234"></property>
</bean>
</beans>
5.data.properties资源
username1=root123
pwd=123
6.TestUser测试类通过spring获取资源(Service实例)
import com.limei.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestUser {
@Test
public void save(){
ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml");
// ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-user.xml","applicationContext-book.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
评论 (0)