Java---学习(7)
来源:互联网 发布:php namespace use 编辑:程序博客网 时间:2024/04/30 10:05
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; }
- java学习(7)
- java学习(7)
- Java---学习(7)
- JAVA基础学习(7)
- java基础学习(7)
- java学习笔记(7)
- Java学习-基础(7)
- java学习(7) 接口
- 【java】java学习(一)
- 【java】java学习(二)
- Java学习笔记18天---(7)
- Java NIO学习7(字符集)
- 我的java学习日记(7)
- java基础学习笔记(7)
- 黑马程序员 -- Java基础学习(7)
- 黑马程序员--java学习总结(7)
- Java 7 NIO.2学习(Ing)
- java基础学习笔记(7)
- HDU--1710--Binary Tree Traversals(二叉树遍历)
- Java---学习(6)
- 51nod 1086 背包问题V2 (巧妙dp,二进制)
- 538. Convert BST to Greater Tree
- 卡尔曼滤波
- Java---学习(7)
- python中hashlib模块
- 链家、CVTE笔试题
- 简单的分布式进程
- POJ 3764:01字典树
- linux文件系统及bash基础特性
- 最长不下降子序列
- 2017. 9.3
- Android源码怎么编译adb?