spring中的類中定義的變量,方法,構造函數通過標註@Autowired註解可以從配置文件中找到相應的bean,完成自動裝配的工作。默認情況下,@Autowired是按類型來匹配相應的bean,也可以通過名稱來匹配,需要設置名稱。
如果想使@Autowired註解生效,需要在配置文件中添加AutowiredAnnotationBeanPostProcessor,通過這個,系統將自動解析bean
- <bean class=“org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor”/>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
1.@Autowired按類型裝配
下面我們先建立一個UserDao類,
- package com.springfirst.Controller;
- public class UserDao {
- public String GetUserName()
- {
- return “Hello World”;
- }
- }
package com.springfirst.Controller;
public class UserDao {
public String GetUserName()
{
return "Hello World";
}
}
IUserService 接口
- package com.springfirst.Controller;
- public interface IUserService {
- public String GetUserName();
- }
package com.springfirst.Controller;
public interface IUserService {
public String GetUserName();
}
UserService類
- package com.springfirst.Controller;
- import org.springframework.beans.factory.annotation.Autowired;
- public class UserService implements IUserService {
- @Autowired
- private UserDao userDao;
- public String GetUserName() {
- // TODO Auto-generated method stub
- return userDao.GetUserName();
- }
- }
package com.springfirst.Controller;
import org.springframework.beans.factory.annotation.Autowired;
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
public String GetUserName() {
// TODO Auto-generated method stub
return userDao.GetUserName();
}
}
在controller中我們定義userService,系統會進行自動匹配裝載,找到userService的具體實現
- @Autowired
- private IUserService userService;
- @RequestMapping(value=“userdetail”)
- public String userdetail(ModelMap model)
- {
- String name=userService.GetUserName();
- System.out.print(name);
- model.addAttribute(”username”,name);
- return “userdetail”;
- }
@Autowired
private IUserService userService;
@RequestMapping(value="userdetail")
public String userdetail(ModelMap model)
{
String name=userService.GetUserName();
System.out.print(name);
model.addAttribute("username",name);
return "userdetail";
}
配置文件
- <?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:context=“http://www.springframework.org/schema/context”
- xmlns:aop=”http://www.springframework.org/schema/aop” xmlns:p=“http://www.springframework.org/schema/p”
- xmlns:cache=”http://www.springframework.org/schema/cache” xmlns:repo=“http://www.springframework.org/schema/data/repository”
- xmlns:tx=”http://www.springframework.org/schema/tx” xmlns:jpa=“http://www.springframework.org/schema/data/jpa”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
- http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd ”
- default-lazy-init=“true”>
- <description>Spring配置</description>
- <!– 該 BeanPostProcessor 將自動起作用,對標註 @Autowired 的 Bean 進行自動注入 –>
- <bean class=“org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor”/>
- <bean id=”userService” class=“com.springfirst.Controller.UserService”/>
- <bean id=”userDao” class=“com.springfirst.Controller.UserDao”/>
- </beans>
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "
default-lazy-init="true">
<description>Spring配置</description>
<!-- 該 BeanPostProcessor 將自動起作用,對標註 @Autowired 的 Bean 進行自動注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="userService" class="com.springfirst.Controller.UserService"/>
<bean id="userDao" class="com.springfirst.Controller.UserDao"/>
</beans>
上面的自動裝配是按類型進行匹配的,Spring會自動從配置文件中找到相應的類型,進行裝載
使用方法進行註解
- private IUserService userService;
- @Autowired()
- public void setUserService(IUserService userService) {
- this.userService = userService;
- }
private IUserService userService;
@Autowired()
public void setUserService(IUserService userService) {
this.userService = userService;
}
使用構造函數進行註解
- private IUserService userService;
- @Autowired
- public UserController(IUserService userService)
- {
- this.userService=userService;
- }
private IUserService userService;
@Autowired
public UserController(IUserService userService)
{
this.userService=userService;
}
2.@Autowired按名稱裝配
按名稱裝載的時候,@Autowired需要配合@Qualifier一起使用,通過@Qualifier可以指定名稱。
如果兩個類同時繼承一個接口,這個時候我們要通過@Qualifier來指定名稱進行匹配,所謂名稱就是XML文件中的bean的id, 這裏我們定義了Employee類和UserService類都繼承IUserService接口
- package com.springfirst.Controller;
- import org.springframework.beans.factory.annotation.Autowired;
- public class Employee implements IUserService {
- public String GetUserName() {
- // TODO Auto-generated method stub
- return “Employee”;
- }
- }
package com.springfirst.Controller;
import org.springframework.beans.factory.annotation.Autowired;
public class Employee implements IUserService {
public String GetUserName() {
// TODO Auto-generated method stub
return "Employee";
}
}
- package com.springfirst.Controller;
- import org.springframework.beans.factory.annotation.Autowired;
- public class UserService implements IUserService {
- @Autowired
- private UserDao userDao;
- public String GetUserName() {
- // TODO Auto-generated method stub
- return userDao.GetUserName();
- }
- }
package com.springfirst.Controller;
import org.springframework.beans.factory.annotation.Autowired;
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
public String GetUserName() {
// TODO Auto-generated method stub
return userDao.GetUserName();
}
}
在xml中定義了bean
- <?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:context=“http://www.springframework.org/schema/context”
- xmlns:aop=“http://www.springframework.org/schema/aop” xmlns:p=“http://www.springframework.org/schema/p”
- xmlns:cache=“http://www.springframework.org/schema/cache” xmlns:repo=“http://www.springframework.org/schema/data/repository”
- xmlns:tx=“http://www.springframework.org/schema/tx” xmlns:jpa=“http://www.springframework.org/schema/data/jpa”
- xsi:schemaLocation=”http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
- http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
- http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
- http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd ”
- default-lazy-init=“true”>
- <description>Spring配置</description>
- <!– 該 BeanPostProcessor 將自動起作用,對標註 @Autowired 的 Bean 進行自動注入 –>
- <bean class=“org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor”/>
- <bean id=“userService” class=“com.springfirst.Controller.UserService”/>
- <bean id=“employee” class=“com.springfirst.Controller.Employee”/>
- <bean id=“userDao” class=“com.springfirst.Controller.UserDao”/>
- </beans>
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:repo="http://www.springframework.org/schema/data/repository"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.7.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd "
default-lazy-init="true">
<description>Spring配置</description>
<!-- 該 BeanPostProcessor 將自動起作用,對標註 @Autowired 的 Bean 進行自動注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="userService" class="com.springfirst.Controller.UserService"/>
<bean id="employee" class="com.springfirst.Controller.Employee"/>
<bean id="userDao" class="com.springfirst.Controller.UserDao"/>
</beans>
- private IUserService userService;
- //這裏我們使用id爲userService的bean
- @Autowired
- public UserController(@Qualifier(“userService”)IUserService userService)
- {
- this.userService=userService;
- }
private IUserService userService;
//這裏我們使用id爲userService的bean
@Autowired
public UserController(@Qualifier("userService")IUserService userService)
{
this.userService=userService;
}