Spring-01 注解实现IOC
Spring框架四大原则
- 使用pojo进行轻量级和最小侵入式开发。
- 通过依赖注入和基于接口编程实现松耦合。
- 使用AOP和默认习惯进行声明式编程。
- 使用AOP和模板(template)减少模式化代码。
控制反转和依赖注入
- Spring通过依赖注入实现控制反转。
- JavaEE项目通过工厂模式实现控制反转。
- Spring的依赖注入原理也是基于工厂模式。
- Spring提供了使用xml、注解、java配置、groovy配置实现依赖注入。
测试环境说明
1.使用myeclipse创建maven项目,jdk基于1.7

2.填写maven项目GAV(三坐标)

3.项目结构

4.pom.xml文件信息
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.etc</groupId>
<artifactId>spring4demo01</artifactId>
<version>1.0.0-SNAPSHOT</version>
<properties>
<java.version>1.7</java.version>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.9.RELEASE</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
常用注解
- 声明bean的注解
| 注解 | 说明 |
| @Component | 声明组件注解,bean没有明确角色。 |
| @Service | 业务逻辑层声明bean组件使用(service层或者biz层)。 |
| @Repository | 数据访问层声明bean组件使用(dao层)。 |
| @Controller | MVC模型中,在控制层(C)声明bean组件层使用。 |
以上注解位于:org.springframework.stereotype
- 注入bean的注解
| 注解 | 说明 |
| @AutoWired | 按照类型装配注入,可以不通过getter和setter访问器注入。 |
| @Qualifier |
通常和@AutoWired注解配合使用。 如果@AutoWired找到多个可以装配类型, 则可以通过@Qualifier注解指定bean名称注入。 用法:@Qualifier("entityDao") |
| @Resource |
JSR-250提供的注解,位于javax.annotation包下。 注入时候默认按照名称注入,如果无法匹配名称,则转换为按照类型注入。 名称指属性名称或者setter访问器方法名。 |
| @Inject | 用法和@AutoWired类似。 |
示例代码
数据访问层代码
package com.etc.dao; import org.springframework.stereotype.Repository; @Repository //数据访问层注解
public class EntityDao { public String getData(){
return "get data from database";
}
}
业务逻辑层代码
package com.etc.service; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.etc.dao.EntityDao; @Service //service层注解
public class EntityService { @Autowired //注入bean
private EntityDao entityDao; public String getData(){
return entityDao.getData();
} }
配置类代码
package com.etc.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration //声明DiConfig类为配置类
@ComponentScan("com.etc.service,com.etc.dao") //扫描service和dao包所有使用注解声明的bean,并创建和注册为spring bean
public class DiConfig { }
测试类
package com.etc.test; import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import com.etc.config.DiConfig;
import com.etc.service.EntityService; public class TestClass { /**测试使用注解实现IOC*/
@Test
public void test1() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
DiConfig.class);
EntityService es = context.getBean(EntityService.class);
System.out.println(es.getData());
context.close();
} }
测试结果
一月 15, 2018 9:22:54 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@442c8ab0: startup date [Mon Jan 15 09:22:54 CST 2018]; root of context hierarchy
get data from database
一月 15, 2018 9:22:54 上午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose
信息: Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@442c8ab0: startup date [Mon Jan 15 09:22:54 CST 2018]; root of context hierarchy
Spring-01 注解实现IOC的更多相关文章
- java框架之Spring(2)-注解配置IOC&AOP配置
注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...
- spring基于注解的IoC以及IoC的案例
1.Spring中IoC的常用注解 1.1明确: (1)基于注解的配置和xml的配置要实现的功能都是一样的,都是要降低程序之间的耦合,只是配置的形式不一样 2.案例:使用xml方式和注解方式实现单表的 ...
- spring常用注解以IOC理解
使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...
- spring基于注解的IOC
曾经的XML配置: <bean id="accountService" class="com.itheima.service.impl.AccountService ...
- Spring(四)注解配置Ioc
原文链接:http://www.orlion.ga/216/ 一.@Autowired beans.xml配置成如下: <?xml version="1.0" encodin ...
- Spring的注解学习(ioc,aop结合)
首先引入jar包 aspectjrt.jar aspectjweaver.jar 1.dao package com.dao; public interface OkpDao { public voi ...
- spring装配注解(IOC容器加载控制)ComponentScan及ComponentScans使用
ComponentScan,只写入value,可扫描路径下装配的@Contrller.@Service.@Repository @ComponentScan(value = "com.tes ...
- Spring 基于注解的 IOC 配置
创建 spring 的 的 xml 配置 文件 <context:component-scan base-package="com.itheim"/> 指定创建容器时要 ...
- Spring IOC的描述和Spring的注解(转)
Spring常用的注解 本文系转载:转载网址: http://www.cnblogs.com/xdp-gacl/p/3495887.html http://ljhzzyx.blog.163.com/b ...
- spring的纯注解的IOC配置
package config; import com.mchange.v2.c3p0.ComboPooledDataSource;import org.apache.commons.dbutils.Q ...
随机推荐
- wxPython学习笔记1
wxpython介绍: wxPython 是 Python 语言的一套优秀的 GUI 图形库,允许 Python 程序员很方便的创建完整的.功能键全的 GUI 用户界面. wxPython 是作为优 ...
- linux下sprintf_s函数的替代(转载)
转自:http://www.cnblogs.com/yeahgis/archive/2013/01/22/2872179.html windows平台下线程安全的格式化字符串函数sprint_s并非标 ...
- Android Studio:layout-sw600dp文件夹中创建activity_main.xml
1.右键res文件夹,新建Android resource directory文件夹 2.在resource type中选择layout 3.将Directory name命名为layout-sw6 ...
- bzoj4472: [Jsoi2015]salesman(树形dp)
Description 某售货员小T要到若干城镇去推销商品,由于该地区是交通不便的山区,任意两个城镇之间都只有唯一的可能经过其它城镇的路线. 小T 可以准确地估计出在每个城镇停留的净收益.这些净收益可 ...
- Codeforces Round #516 Div2 (A~D)By cellur925
比赛传送门 A. Make a triangle! 题目大意:给你三根木棒,选出其中一根木棒增加它的长度,使构成三角形,问增加的长度最小是多少. 思路:签到题,根据样例/三角形性质不难发现,答案就是最 ...
- Luogu P2278 [HNOI2003]操作系统【优先队列/重载运算符/模拟】 By cellur925
题目传送门 本来是照着二叉堆的题去做的...没想到捡了个模拟...不过模拟我都不会...我好弱啊... 其实核心代码并不长,比辰哥的标程短到不知哪里去...但是思路需要清晰. 读题的时候我明白,当有优 ...
- 浅谈Floyd的三种用法 By cellur925
Floyd大家可能第一时间想到的是他求多源最短路的n³算法.其实它还有另外两种算法的嘛qwq.写一发总结好了qwq. 一.多源最短路 放段代码跑,注意枚举顺序,用邻接矩阵存图.本质是一种动规. 复杂度 ...
- hdu1811 Rank of Tetris 并查集+拓扑排序
#include <stdio.h> #include <string.h> #include <vector> #include <queue> us ...
- LightOj 1076 - Get the Containers (折半枚举好题)
题目链接: http://www.lightoj.com/volume_showproblem.php?problem=1076 题目描述: 给出n个数,要求分成m段,问这m段中最大的总和,最小是多少 ...
- Educational Codeforces Round 24 E
Vova again tries to play some computer card game. The rules of deck creation in this game are simple ...