Spring自动装配

Posted popcorn丫

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring自动装配相关的知识,希望对你有一定的参考价值。

实体类:

技术分享
 1 package com.bxw.po;
 2 
 3 public class User {
 4     private String username;    //账号
 5     private String password;    //密码
 6     public String getUsername() {
 7         return username;
 8     }
 9     public void setUsername(String username) {
10         this.username = username;
11     }
12     public String getPassword() {
13         return password;
14     }
15     public void setPassword(String password) {
16         this.password = password;
17     }
18     
19     
20 }
User.java

 

Dao:

技术分享
1 package com.bxw.dao;
2 
3 import com.bxw.po.User;
4 
5 public interface UserDao {
6     public void save(User u);    
7 }
UserDao.java

 

DaoImpl:

技术分享
 1 package com.bxw.dao.impl;
 2 
 3 import com.bxw.dao.UserDao;
 4 import com.bxw.po.User;
 5 
 6 public class UserDaoImpl implements UserDao {
 7     public void save(User u){
 8         System.out.println("user saved");
 9     }
10 }
UserDaoImpl.java

 

UserService:

技术分享
 1 package com.bxw.service;
 2 
 3 import com.bxw.dao.UserDao;
 4 import com.bxw.dao.impl.UserDaoImpl;
 5 import com.bxw.po.User;
 6 
 7 public class UserService {
 8     private UserDao userDao = new UserDaoImpl();
 9 
10     public UserDao getUserDao() {
11         return userDao;
12     }
13 
14     public void setUserDao(UserDao userDao) {
15         this.userDao = userDao;
16     }
17 
18     
19     public void add(User u){
20         this.userDao.save(u);
21     }
22     
23 }
UserService.java

 

applicationContext.xml:

技术分享
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
 7     
 8     <bean id="userDao" class="com.bxw.dao.impl.UserDaoImpl" />
 9     <bean id="userService" class="com.bxw.service.UserService" autowire="byName">
10     </bean>
11 
12 </beans>
applicationContext.xml

 

UserServiceTest.java:

技术分享
 1 package com.bxw.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.bxw.po.User;
 8 import com.bxw.service.UserService;
 9 
10 public class UserServiceTest {
11 
12     @Test
13     public void testAdd() throws Exception{
14 //        BeanFactory ac = new ClassPathXMLApplicationContext();    //初始化所有bean
15 //        UserService service = (UserService) ac.getBean("userService");    //拿到id为userService,依赖注入userDao
16         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
17         UserService service = (UserService) ac.getBean("userService");
18         User u = new User();
19         service.add(u);
20     }
21 }
UserServiceTest.java

 

autowire常见取值有byname,bytype(其余取值不常用)。如果取值为byName,则UserService会去找名为UserDao的bean;如果取值为byType,则UserService则会去找与UserDao类型相同的bean。如果取值为defaule,则先byName,后byType。

以上是关于Spring自动装配的主要内容,如果未能解决你的问题,请参考以下文章

Spring5 Java代码装配Bean

Spring5 自动化装配Bean

8 -- 深入使用Spring -- 7...4 使用自动装配

spring:按照Bean的名称自动装配User

Spring XML配置里的Bean自动装配

spring 自动装配 bean 有哪些方式?