Spring框架四大原则

  1. 使用pojo进行轻量级和最小侵入式开发。
  2. 通过依赖注入和基于接口编程实现松耦合。
  3. 使用AOP和默认习惯进行声明式编程。
  4. 使用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的更多相关文章

  1. java框架之Spring(2)-注解配置IOC&AOP配置

    注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...

  2. spring基于注解的IoC以及IoC的案例

    1.Spring中IoC的常用注解 1.1明确: (1)基于注解的配置和xml的配置要实现的功能都是一样的,都是要降低程序之间的耦合,只是配置的形式不一样 2.案例:使用xml方式和注解方式实现单表的 ...

  3. spring常用注解以IOC理解

    使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan base-package ...

  4. spring基于注解的IOC

    曾经的XML配置: <bean id="accountService" class="com.itheima.service.impl.AccountService ...

  5. Spring(四)注解配置Ioc

    原文链接:http://www.orlion.ga/216/ 一.@Autowired beans.xml配置成如下: <?xml version="1.0" encodin ...

  6. Spring的注解学习(ioc,aop结合)

    首先引入jar包 aspectjrt.jar aspectjweaver.jar 1.dao package com.dao; public interface OkpDao { public voi ...

  7. spring装配注解(IOC容器加载控制)ComponentScan及ComponentScans使用

    ComponentScan,只写入value,可扫描路径下装配的@Contrller.@Service.@Repository @ComponentScan(value = "com.tes ...

  8. Spring 基于注解的 IOC 配置

    创建 spring 的 的 xml 配置 文件 <context:component-scan base-package="com.itheim"/> 指定创建容器时要 ...

  9. Spring IOC的描述和Spring的注解(转)

    Spring常用的注解 本文系转载:转载网址: http://www.cnblogs.com/xdp-gacl/p/3495887.html http://ljhzzyx.blog.163.com/b ...

  10. spring的纯注解的IOC配置

    package config; import com.mchange.v2.c3p0.ComboPooledDataSource;import org.apache.commons.dbutils.Q ...

随机推荐

  1. CClientDC类 CWindowDC类

    CClientDC类 CClientDC类也是CDC类的派生类.它只能在窗口的客户区(即窗口中除了边框.标题栏.菜单栏以及状态栏外的中间部分)中进行绘图,坐标点(0,0)通常指的是客户区的左上角.它的 ...

  2. Collection View Programming Guide for iOS---(六)---Creating Custom Layouts

    Creating Custom Layouts 创建自定义布局 Before you start building custom layouts, consider whether doing so ...

  3. Android Service完全解析,关于服务你所需知道的一切(下) (转载)

    转自:http://blog.csdn.net/guolin_blog/article/details/9797169 转载请注册出处:http://blog.csdn.net/guolin_blog ...

  4. 洛谷 - P1219 - 八皇后 - dfs

    https://www.luogu.org/problemnew/show/P1219 一开始朴素检查对角线就TLE了,给对角线编码之后压缩了13倍时间? 找了很久的bug居然是&&写 ...

  5. python 字典 dict items values keys

    dict.items() 1 >>> d = dict(one=1,two=2) 2 >>> it1 = d.items() 3 >>> it1 ...

  6. 【Tip】Python

    『基本操作』 [查看Python所在目录] import os print(os.__file__) [查看已安装的包] pip list [获取当前脚本所在目录] import sys import ...

  7. hdu 3172 Virtual Friends (字符串的并查集)

    一开始一直wa,因为第一个数字t也是多组输入. 然后一直超时,因为我用的是C++里面的cin,所以非常耗时,几乎比scanf慢了10倍,但是加上了一个语句后: std::ios::sync_with_ ...

  8. eclipse | 配置JRE

    Window --> Preference --> Java ---> Installed JREs

  9. python之yaml模块和ddt模块

    aml文件是专门用来写配置文件的语言,非常简洁和强大,远比json格式方便. 在PC中新建一个yml/yaml为为缩略名的文件,输入信息见下图 新建一个py文件处理yml文件,直接处理成字典格式 缩进 ...

  10. Hdu 5439 Aggregated Counting (2015长春网络赛 ACM/ICPC Asia Regional Changchun Online 找规律)

    题目链接: Hdu 5439 Aggregated Counting 题目描述: 刚开始给一个1,序列a是由a[i]个i组成,最后1就变成了1,2,2,3,3,4,4,4,5,5,5.......,最 ...