UserService
public interface UserService { public void save(); }
UserServiceImpl
public class UserServiceImpl implements UserService { @Override public void save() { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = (UserDao) app.getBean("userDao"); userDao.save(); } }
<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl"></bean>
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }
依赖注入(Dependency Injection):它是 Spring 框架核心 IOC 的具体实现。
在编写程序时,通过控制反转,把对象的创建交给了 Spring,但是代码中不可能出现没有依赖的情况。
IOC 解耦只是降低他们的依赖关系,但不会消除。例如:业务层仍会调用持久层的方法。
那这种业务层和持久层的依赖关系,在使用 Spring 之后,就让 Spring 来维护了。
简单的说,就是坐等框架把持久层对象传入业务层,而不用我们自己去获取。
怎么将UserDao怎样注入到UserService内部呢?
1)set方法注入
在UserServiceImpl中添加setUserDao方法
public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); } }
配置Spring容器调用set方法进行注入
<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
1)set方法注入
P命名空间注入本质也是set方法注入,但比起上述的set方法注入更加方便,主要体现在配置文件中,如下:
首先,需要引入P命名空间:
xmlns:p="http://www.springframework.org/schema/p"
其次,需要修改注入方式
<bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl" p:userDao-ref="userDao"/>
2)构造方法注入
创建有参构造
public class UserServiceImpl implements UserService { private UserDao userDao; public UserServiceImpl() { } public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); } }
<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"></bean> <bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
上面的操作,都是注入的引用Bean,除了对象的引用可以注入,普通数据类型,集合等都可以在容器中进行注入。
注入数据的三种数据类型
普通数据类型
引用数据类型
集合数据类型
其中引用数据类型,此处就不再赘述了,之前的操作都是对UserDao对象的引用进行注入的,下面将以set方法注入为例,演示普通数据类型和集合数据类型的注入。
public class UserDaoImpl implements UserDao { private String username; private int age; public void setUsername(String username) { this.username = username; } public void setAge(int age) { this.age = age; } @Override public void save() { System.out.println("username=" + username + "\n" + "age=" + age); System.out.println("save running......"); } }
<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"> <property name="username" value="xdr"/> <property name="age" value="22"/> </bean>
List <String>
)的注入public interface UserDao { public void save(); }
public class UserDaoImpl implements UserDao { private List<String> strList; public void setStrList(List<String> strList) { this.strList = strList; } public void save() { System.out.println(strList); System.out.println("save running......"); }
public interface UserService { public void save(); }
public class UserServiceImpl implements UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public UserServiceImpl() { } public UserServiceImpl(UserDao userDao) { this.userDao = userDao; } @Override public void save() { userDao.save(); } }
List<String>
是普通数据类型,用 value,如果是 User 或其他对象用 ref<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"> <property name="strList"> <list> <value>aaa</value> <value>bbb</value> <value>ccc</value> </list> </property> </bean> <bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }
Map<String User>
)的注入public class User { private String name; private String addr; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddr() { return addr; } public void setAddr(String addr) { this.addr = addr; } @Override public String toString() { return "User{" + "name='" + name + '\'' + ", addr='" + addr + '\'' + '}'; } }
public class UserDaoImpl implements UserDao { private List<User> userList; public void setUserList(List<User> userList) { this.userList = userList; } public void save() { System.out.println(userList); System.out.println("save running......"); } }
<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"> <property name="userMap"> <map> <entry key="u1" value-ref="user1"></entry> <entry key="u2" value-ref="user2"></entry> </map> </property> </bean> <bean id="user1" class="com.xdr630.domain.User"> <property name="name" value="tom"/> <property name="addr" value="beijing"/> </bean> <bean id="user2" class="com.xdr630.domain.User"> <property name="name" value="jerry"/> <property name="addr" value="shanghai"/> </bean> <bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }
<bean id="userDao" class="com.xdr630.dao.impl.UserDaoImpl"> <property name="properties"> <props> <prop key="p1">ppp1</prop> <prop key="p2">ppp2</prop> <prop key="p3">ppp3</prop> </props> </property> </bean> <bean id="userService" class="com.xdr630.service.Impl.UserServiceImpl"> <constructor-arg name="userDao" ref="userDao"></constructor-arg> </bean>
public class UserDaoImpl implements UserDao { private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } public void save() { System.out.println(properties); System.out.println("save running......"); } }
public class UserController { public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = (UserService) app.getBean("userService"); userService.save(); } }
实际开发中,Spring的配置内容非常多,这就导致Spring配置很繁杂且体积很大,所以,可以将部分配置拆解到其他配置文件中,而在Spring主配置文件通过import标签进行加载
如:在主配置文件中可以引用其他分模块的配置文件
通过如下引用:
<import resource="applicationContext-xxx.xml"/>
<bean>标签 id属性:在容器中Bean实例的唯一标识,不允许重复 class属性:要实例化的Bean的全限定名 scope属性:Bean的作用范围,常用是Singleton(默认)和prototype <property>标签:属性注入 name属性:属性名称 value属性:注入的普通属性值 ref属性:注入的对象引用值 <list>标签 <map>标签 <properties>标签 <constructor-arg>标签 <import>标签:导入其他的Spring的分文件