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 ...
随机推荐
- 探究final在java中的作用
目录 一. final修饰变量 1. 基础: final修饰基本数据类型变量和引用数据类型变量. 2. 进阶: 被final修饰的常量在编译阶段会被放入常量池中 3. 探索: 为什么局部/匿名内部类在 ...
- TP5之model
使用model 查询数据,添加数据,修改数据,删除数据 聚合操作 获取器,修改器 自动添加时间戳(创建时间,修改时间) 软删除 1.使用model查询数据 $res = User::get(1); / ...
- 枚举与#define 宏的区别
1),#define 宏常量是在预编译阶段进行简单替换.枚举常量则是在编译的时候确定其值.2),一般在编译器里,可以调试枚举常量,但是不能调试宏常量.3),枚举可以一次定义大量相关的常量,而#defi ...
- POJ2105【进制转化】
直接瞎写就可以水过.我记得STL有很多好的函数,哎.水过去补多校的题. //#include <bits/stdc++.h> #include<cstdio> #include ...
- HDU1016【简单递归.DFS】
题意:一个环,相邻相加是素数. 思路: 直接深搜就好了.. output limit exceed 了好几发... 因为那个while里面的scanf前面的"~" 后来搜了outp ...
- .NET Core 跨平台物联网开发:设置委托事件(二)
系列教程目录 (一) 连接阿里云IOT (二) 设置委托事件 (三) 上报属性 (四) SDK文档 属性.方法.委托.类 http://pan.whuanle.cn/index.php?dir=up ...
- Nginx(四) nginx+consul+upasync 在ubnutu18带桌面系统 实现动态负载均衡
1.1 什么是动态负载均衡 传统的负载均衡,如果Upstream参数发生变化,每次都需要重新加载nginx.conf文件,因此扩展性不是很高,所以我们可以采用动态负载均衡,实现Upstream可配置化 ...
- Python 爬虫面试题 170 道:2019 版
引言 最近在刷面试题,所以需要看大量的 Python 相关的面试题,从大量的题目中总结了很多的知识,同时也对一些题目进行拓展了,但是在看了网上的大部分面试题不是很满意,一个是有些部分还是 Python ...
- 力荐!35 个最好用的 Vue 开源库!
无论是开发新手还是经验丰富的老手,我们都喜欢开源软件包.对于开发者来说,如果没有这些开源软件包,很难想象我们的生活会变得多么疲惫不堪,而且靠咖啡度日也会成为家常便饭.所幸的是,随着 Vue.js 和 ...
- 《统计学习方法》笔记三 k近邻法
本系列笔记内容参考来源为李航<统计学习方法> k近邻是一种基本分类与回归方法,书中只讨论分类情况.输入为实例的特征向量,输出为实例的类别.k值的选择.距离度量及分类决策规则是k近邻法的三个 ...