IOC操作Bean管理(基于注解方式)
1.什么是注解(1)注解是代码的特殊标记, 格式 @注解名(属性名=值,属性2=值2)(2)使用注解,注解作用在 类上面,方法上,属性上(3)使用注解目的: 简化xml配置2.spring针对bean 管理中创建对象提供注解(1) @Component (2) @Service(3) @Controller(4) @Repository上面的四个注解功能是一样的,都可以用来创建对象3.基于注解方式实现对象的创建(1) 引入依赖 spring-aop-.jar(2) 开启组件扫描: 先引入context命名空间<!--开启 组件扫描包如果 扫描多个包,多个包使用逗号隔开扫描包上层目录--><context:component-scan base-package="com.ly"/>
(3)创建类, 在类上 添加 之前4个注解之一package com.ly.service;import org.springframework.stereotype.Component;//注解里面value属性值可以省略不写,//默认值是 类名,首字母小写//UserService--->userService@Component // <bean id="userService">public class UserService {public void add(){System.out.println("--UserService--------add");}}测试@Testpublic void testMyBean(){ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");UserService s = ac.getBean("userService",UserService.class);s.add();}
//控制台 输出--UserService--------add
开启组件扫描细节配置
<!--use-default-filters="false" 表示现在不使用默认filter 自己配置filtercontext:include-filter 设置 扫描哪些内容--><!--扫描 com.ly 包下 带有Controller 注解的类--><context:component-scan base-package="com.ly" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!--context:exclude-filter 设置 哪些内容不进行扫描--><context:component-scan base-package="com.ly" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan>
基于注解方式实现属性注入
1.@AutoWired:根据属性 类型 进行自动装配
a. 把 service和dao对象创建,在service和dao类添加创建对象的注解
b.在service中注入dao,在service类添加dao类型属性,并添加@AutoWired ,(不需要set方法)
package com.ly.service;import com.ly.dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;//注解里面value属性值可以省略不写,//默认值是 类名,首字母小写//UserService--->userService@Servicepublic class UserService {@Autowired //根据类型注入private UserDao userDao;public void add(){userDao.add();System.out.println("--UserService--------add");}}
package com.ly.dao;public interface UserDao {public void add();}package com.ly.dao.impl;import com.ly.dao.UserDao;import org.springframework.stereotype.Repository;@Repositorypublic class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("dao add ......");}}
<!--开启 组件扫描包如果 扫描多个包,多个包使用逗号隔开扫描包上层目录--><context:component-scan base-package="com.ly"/>
测试类@Testpublic void testMyBean(){ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");UserService s = ac.getBean("userService",UserService.class);s.add();}
控制台输出dao add ......--UserService--------add
2.@Qualifier:根据属性名称 进行注入
@Qualifier和@Autowired 一起使用
一个接口 可以有多个实现类, 当 使用时 就可以通过 @Qualifier和@Autowired 指定 名字 来指定具体实现package com.ly.dao.impl;import com.ly.dao.UserDao;import org.springframework.stereotype.Repository;@Repository(value = "userDao1")public class UserDaoImpl implements UserDao {@Overridepublic void add() {System.out.println("dao add ......");}}
package com.ly.dao;public interface UserDao {public void add();}
package com.ly.service;import com.ly.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service;//注解里面value属性值可以省略不写, //默认值是 类名,首字母小写 //UserService--->userService @Service public class UserService {@Autowired //根据类型注入@Qualifier(value = "userDao1")private UserDao userDao;public void add(){userDao.add();System.out.println("--UserService--------add");} }
@Testpublic void testMyBean(){ApplicationContext ac = new ClassPathXmlApplicationContext("bean1.xml");UserService s = ac.getBean("userService",UserService.class);s.add();}
控制台输出dao add ......--UserService--------add
3.@Resource: 可以根据类型注入,也可以根据名称注入
javax.annotation.Resource 是 java中的扩展包, 而不是spring中的, ,因此 spring 官方不太推荐使用
package com.ly.service;import com.ly.dao.UserDao;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;import org.springframework.stereotype.Service;import javax.annotation.Resource;//注解里面value属性值可以省略不写,//默认值是 类名,首字母小写//UserService--->userService@Servicepublic class UserService {@Resource //根据类型注入 // 改为 @Resource(name = "userDaoImpl") 则是按照 名称 注入private UserDao userDao;public void add(){userDao.add();System.out.println("--UserService--------add");}}
4.@Value 注入普通类型
@Value(value="nihao")private String name; // 此时 name 属性的值 就是 nihao
纯注解开发
//1. 创建配置类,替代xml配置文件package com.ly.config;import org.springframework.context.annotation.ComponentScan;import org.springframework.context.annotation.Configuration;@Configuration //创建配置类 ,替代xml配置文件@ComponentScan(basePackages = {"com.ly"}) //配置扫描包 信息public class SpringConfig {}
//2.编写测试类@Testpublic void testMyBean2(){ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);// 加载配置类UserService s = ac.getBean("userService",UserService.class);s.add();}