注解注入demo
需要做如下准备:
1.加入jar包 spring-aop-5.0.11.RELEASE.jar
2.增加约束文件
3.注册组件扫描器,为了让spring找到注解的位置
4.类上加注解
package com.cn.dao; import org.springframework.stereotype.Component; /**@Component 放在类上,作用是创建类的对象 * 有value 属性 * 相当于: <bean id="dao" class="com.cn.dao.DoSomeThingsOracleDao" /> * 调用无参的构造函数创建对象 */ @Component("dao") public class DoSomeThingsOracleDao implements SQLDao{ public void insert(){ System.out.println("oracle do insert"); } } package com.cn.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.cn.dao.DoSomeThingsOracleDao; @Component("doSomeService") public class DoSomeThingsService { @Resource private DoSomeThingsOracleDao dao; @Override public String toString() { return "DoSomeThingsService [dao=" + dao + "]"; } } <?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <!-- 使用注解的方式实现DI 1.加入jar包 spring-aop-5.0.11.RELEASE.jar 2.增加约束文件 3.注册组件扫描器,为了让spring找到注解的位置 4.类上加注解 --> <!-- base-package 指定注解所在的包名 com.cn 扫描这个包下及其子包下的所有类 --> <context:component-scan base-package="com.cn"/> </beans> package com.cn.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.cn.service.DoSomeThingsService; public class Test { public static void main(String[] args) { String resource = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); DoSomeThingsService service= (DoSomeThingsService) ac.getBean("doSomeService"); System.out.println(service.toString()); } } DoSomeThingsService [dao=com.cn.dao.DoSomeThingsOracleDao@80169cf]
如果 @Component 不赋value值的话,默认beanid是类的首字母小写
package com.cn.service; import javax.annotation.Resource; import org.springframework.stereotype.Component; import com.cn.dao.DoSomeThingsOracleDao; @Component public class DoSomeThingsService { @Resource private DoSomeThingsOracleDao dao; @Override public String toString() { return "DoSomeThingsService [dao=" + dao + "]"; } } public static void main(String[] args) { String resource = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); DoSomeThingsService service= (DoSomeThingsService) ac.getBean("doSomeThingsService"); System.out.println(service.toString()); }
指定包的方式
有三种
逗号分隔 <context:component-scan base-package="com.cn.dao,com.cn.service"/> 使用多个标签 <context:component-scan base-package="com.cn.dao"/><context:component-scan base-package="com.cn.service"/> 使用父包 <context:component-scan base-package="com.cn"/>
@Scope("prototype")
package com.cn.service; import javax.annotation.Resource; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.cn.dao.DoSomeThingsOracleDao; @Component @Scope("prototype") public class DoSomeThingsService { @Resource private DoSomeThingsOracleDao doSomeThingsOracleDao; public DoSomeThingsService() { System.out.println("service sonstruction"); } @Override public String toString() { return "DoSomeThingsService [dao=" + doSomeThingsOracleDao + "]"; } } public static void main(String[] args) { String resource = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); DoSomeThingsService service= (DoSomeThingsService) ac.getBean("doSomeThingsService"); DoSomeThingsService service1= (DoSomeThingsService) ac.getBean("doSomeThingsService"); // System.out.println(service.toString()); } service sonstruction service sonstruction
和@Component 类似的注解
和@Component 类似的注解有 @service,@Repository,@Controller。
@service是放在service层的实体类上。创建service对象
@Repository是放在dao层的
@Controller 放在处理器类上的。servlet。
package com.cn.dao; import org.springframework.stereotype.Repository; /**@Component 放在类上,作用是创建类的对象 * 有value 属性 * 相当于: <bean id="dao" class="com.cn.dao.DoSomeThingsOracleDao" /> * 调用无参的构造函数创建对象 */ @Repository public class DoSomeThingsOracleDao implements SQLDao{ public void insert(){ System.out.println("oracle do insert"); } } package com.cn.service; import javax.annotation.Resource; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import com.cn.dao.DoSomeThingsOracleDao; @Component //@Scope("prototype") public class DoSomeThingsService { @Value("6") private int valInt; @Resource(name="doSomeThingsOracleDao") private DoSomeThingsOracleDao dao; /** * 区别:@Resource 是JDK的,默认按byName注入,注入失败,再按照byType注入。 * Resource也可以只按照byName注入。"@Resource(name="doSomeThingsOracleDao")" * @Autowired 是spring的,默认是按照byType注入的,需要按照byName注入的话,需要配合@Qualifier一起使用 * @Autowired 必须注入成功。因为 "boolean required() default true;" * */ @Autowired @Qualifier("doSomeThingsOracleDao") private DoSomeThingsOracleDao dao1; public DoSomeThingsService() { System.out.println("service sonstruction"); } @Override public String toString() { return "DoSomeThingsService [valInt=" + valInt + ", dao=" + dao + ", dao1=" + dao1 + "]"; } }
@PostConstruct @PreDestroy
@PostConstruct public void initA() { System.out.println("initA"); } @PreDestroy public void preDestroyA() { System.out.println("destroyA"); } service sonstruction initA DoSomeThingsService [valInt=6, dao=com.cn.dao.DoSomeThingsOracleDao@3e77a1ed, dao1=com.cn.dao.DoSomeThingsOracleDao@3e77a1ed] 七月 27, 2019 2:19:49 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose 信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@c4437c4: startup date [Sat Jul 27 14:19:49 CST 2019]; root of context hierarchy destroyA
注解相对配置文件,代码量少。但是需要修改代码
在没有源码的时候,用别的类时,只能用配置文件实现。
如果代码中一个类既有注解,又有配置文件,以配置文件为准。配置文件优先级高于注解。
<?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" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" > <!-- 使用注解的方式实现DI 1.加入jar包 spring-aop-5.0.11.RELEASE.jar 2.增加约束文件 3.注册组件扫描器,为了让spring找到注解的位置 4.类上加注解 --> <!-- base-package 指定注解所在的包名 com.cn 扫描这个包下及其子包下的所有类 --> <context:component-scan base-package="com.cn"/> <bean id ="service" class="com.cn.service.DoSomeThingsService"/> </beans> public static void main(String[] args) { String resource = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(resource); DoSomeThingsService service= (DoSomeThingsService) ac.getBean("doSomeThingsService"); DoSomeThingsService service1= (DoSomeThingsService) ac.getBean("service"); System.out.println(service.toString()); System.out.println(service1.toString()); } service sonstruction initA service sonstruction initA DoSomeThingsService [valInt=6, dao=com.cn.dao.DoSomeThingsOracleDao@25359ed8, dao1=com.cn.dao.DoSomeThingsOracleDao@25359ed8] DoSomeThingsService [valInt=6, dao=com.cn.dao.DoSomeThingsOracleDao@25359ed8, dao1=com.cn.dao.DoSomeThingsOracleDao@25359ed8] 明显是注入了两个一样的类。。