01点睛Spring4.1-依赖注入
转载:https://www.iteye.com/blog/wiselyman-2210252
1.1 声明bean
- 使用上例建立的testMavenSpring项目,将pom.xml文件中的
<spring-framework.version>3.2.3.RELEASE</spring-framework.version>修改为4.1.5.RELEASE, 然后项目->右键->maven->update project; - spring利用@Configuration,@Component,@Service,@Repository,@Controller注解在一个java类上声明是spring容器的bean;
- 使用@Configuration,@Component,@Service,@Repository,@Controller任意一个在类上效果是等同的,不同的名称是为了更好的标志类的角色和功能,避免代码维护者混淆代码作用;
- 那我们使用这四个注解的准则是什么呢?
- @Configuration 声明是一个配置bean
- @Component 系统组件,没有明确的角色;
- @Service 在业务逻辑层(service层)使用;
- @Repository 在数据访问层(dao层)使用;
- @Controller 在展现层(MVC->Spring MVC)使用;
1.2 注入bean
- 可以使用@Autowired,@Inject(JSR-330),@Resource(JSR-250)注入用@Component,@Service,@Repository,@Controller(当然包括xml声明的或者用@Bean声明的bean)声明的bean;
- @Autowired,@Inject,@Resource在一般情况下是通用的;
- @Autowired,@Inject,@Resource可注解在set方法上或者属性上;我习惯注解在属性上,代码更少层次更清晰;
1.3 示例
1.3.1 新建待注入的java类
package com.wisely.di; import org.springframework.stereotype.Service; @Service//写为@Component,@Controller,@Repository效果相同,视具体情况使用
public class Demo1Service { public String sayHello(String word ){
return "Hello "+word;
}
}
1.3.2 新建被注入的java类
package com.wisely.di; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class Demo2Service {
@Autowired //注入Demo1Service,还可使用JavaEE的@Inject(JSR-330),@Resource(JSR-250)效果相同
Demo1Service demo1Service;
public String callDemo1SayHello(String word){
return demo1Service.sayHello(word);
} }
1.3.3 测试
package com.wisely.di;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
//设定此包下的类被注册成spring的bean,包含@Configuration,@Component,@Service,@Repository,@Controller
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext("com.wisely.di");
Demo2Service demo2Service = context.getBean(Demo2Service.class);
System.out.println(demo2Service.callDemo1SayHello("World"));
context.close();
}
}
输出结果Hello World
01点睛Spring4.1-依赖注入的更多相关文章
- 转载--浅谈spring4泛型依赖注入
转载自某SDN-4O4NotFound Spring 4.0版本中更新了很多新功能,其中比较重要的一个就是对带泛型的Bean进行依赖注入的支持.Spring4的这个改动使得代码可以利用泛型进行进一步的 ...
- spring4之依赖注入的三种方式
1.Setter注入 <bean id="helloWorld" class="com.jdw.spring.beans.HelloWorld"> ...
- spring4笔记----依赖注入的两种形式
设值注入:通过<property.../>元素驱动Spring执行setter的方法 构造注入:通过<constructor-arg.../>元素驱动Spring执行带有参数的 ...
- Spring下的@Order和@Primary与javax.annotation-api下@Priority【Spring4.1后】等方法控制多实现的依赖注入(转)
@Order 可以作用在类.方法.属性. 影响加载顺序. 若不加,spring的加载顺序是随机的. @Primary 当注入bean冲突时,以@Primary定义的为准. @Order是控制配置类的加 ...
- spring4——IOC之基于注解的依赖注入(DI )
spring容器对于Bean的创建和对象属性的依赖注入提供了注解的支持,让我们在开发中能够更加便捷的实现对象的创建和对象属性的依赖注入.一,对于Bean的创建spring容器提供了以下四个注解的支持: ...
- 峰Spring4学习(2)依赖注入的几种方式
一.装配一个bean 二.依赖注入的几种方式 com.cy.entity People.java: package com.cy.entity; public class People { pri ...
- Spring4笔记5--基于注解的DI(依赖注入)
基于注解的DI(依赖注入): 对于 DI 使用注解,将不再需要在 Spring 配置文件中声明 Bean 实例.只需要在 Spring 配置文件中配置组件扫描器,用于在指定的基本包中扫描注解. < ...
- Spring4笔记4--基于XML的DI(依赖注入)
基于XML的DI(依赖注入): Bean 实例在调用无参构造器创建了空值对象后,就要对 Bean 对象的属性进行初始化.初始化是由容器自动完成的,称为注入.根据注入方式的不同,常用的有两类:设值注入. ...
- Spring4 -03 -Dependency Injection (依赖注入) : 代码体现/配置xml/测试
DI:中文名称:依赖注入 英文名称((Dependency Injection) DI 是什么? 3.1 DI 和IoC 是一样的,差不多一样的技术和模板! 3.2 当一个类(A)中需要依赖另一个类( ...
随机推荐
- noi.ac #37 dp计数
#include<algorithm> #include<cstring> #include<cstdio> #include<iostream> ty ...
- P5590 【赛车游戏】
果然我还是太\(Naive\)了 首先有一些点/边其实是没有意义的,如果从1出发不能到该点或者从该点不能到n,这个点就可以不用管了.这个过程可以用正反两边\(dfs/bfs\)实现 然后删掉那些点之后 ...
- python时间序列数据的对齐和数据库的分批查询
欲直接下载代码文件,关注我们的公众号哦!查看历史消息即可! 0. 前言 在机器学习里,我们对时间序列数据做预处理的时候,经常会碰到一个问题:有多个时间序列存在多个表里,每个表的的时间轴不完全相同,要如 ...
- UDP 区别于 TCP 的特点
TCP 我们了解得多了,所以今天我们站在 UDP 的角度,探讨一下 UDP 区别于 TCP 的特点. 1. 面向无连接 UDP 比 TCP 简单得多,不需要“三次握手”来建立连接,直接把内容发送出去. ...
- OpenFOAM当中监测力和阻力系数
首先准备好我们自己的平常算例文件,本次我们以圆柱绕流的算例来说明用法 我们找到constant文件夹 打开其中的transportProperties文件 我们将其中的: nu ...
- springboot项目获取resource下的文件
package com.expr.exceldemo; import org.springframework.core.io.ClassPathResource; public class Test ...
- .netFramework 升级NetCore 问题汇总及解决方案
升级版本: NetCore sdk 2.2.108 .AspNetCore 2.2.0.EFCore 2.2.6 所有程序引用均从NuGet上下载,并支持NetCore 问题: 问题1:No coer ...
- EasyTrader踩坑之旅(三)
快速阅读 用THSTrader 调试同花顺自动下单的过程 . 主要原理是利用python函数pywinauto 自动获取同花顺上相应控件的值,进行模拟自动化的操作,不得不说python函数库的强大 ...
- game-hacking
https://github.com/dsasmblr/game-hacking Cheat Engine Hacking memory Cheat engine have a feature cal ...
- The Matrix | 黑客帝国
今天又刷了一遍,依旧跟第一次看一样,非常惊叹震撼,同时也发现了更多的细节. 梳理一下情节: 开始就是Trinity在matrix里被黑衣人Agent追杀,Trinity团队的目的是寻找Neo,显然Ag ...