01点睛Spring4.1-依赖注入

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

 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
相关介绍 1. spring-aop-4.1.1.RELEASE.jar Spring面向切面编程,提供AOP实现。Spring Beans之上将横切关注点模块化 2. spring-aspects-4.1.1.RELEASE.jar 提供的对AspectJ框架的整合,也是A面向切面编程。 AspectJ可用于基于普通Java对象的模块化 注意:aop 和 aspects区别: http://www.oschina.net/translate/comparative_analysis_between_spring_aop_and_aspectj。 3. spring-beans-4.1.1.RELEASE.jar IOC的基础实现。 配置文件 创建和管理bean。 4.spring-context-4.1.1.RELEASE.jar 在基础IOC功能上提供扩展服务,此外还提供许多企业级服务的支持,有邮件服务、任务调度、JNDI定位,EJB集成、远程访问、缓存以及多种视图层框架的支持。这个jar 文件为Spring 核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,JDNI 所需的全部类,instrumentation组件以及校验Validation 方面的相关类。 外部依赖spring-beans, (spring-aop)。 5. spring-context-support-4.1.1.RELEASE.jar 这个jar文件包含支持缓存Cache(ehcache)、JCA、JMX、邮件服务(Java Mail、COS Mail)、任务计划Scheduling(Timer、Quartz)方面的类。 UI方面的用来与模板(Templating)引擎如 Velocity、FreeMarker、JasperReports集成的类, 6. spring-core-4.1.1.RELEASE.jar spring核心包 7. spring-expression-4.1.1.RELEASE.jar spring表达语言 SpEL以"#{...}"进行标识。 8. spring-instrument-4.1.1.RELEASE.jar Spring对服务器的代理接口 9.spring-instrument-tomcat-4.1.1.RELEASE.jar Spring对tomcat连接池的集成 10. spring-jdbc-4.1.1.RELEASE.jar 对jdbc简单封装 11.spring-jms-4.1.1.RELEASE.jar 简单封装jms api接口 jms: Java消息服务(Java Message Service)应用程序接口 12. spring-messaging-4.1.1.RELEASE.jar 消息发送 13. spring-orm-4.1.1.RELEASE.jar 14. spring-oxm-4.1.1.RELEASE.jar Spring对于object/xml映射的支持,可以让JAVA与XML之间来回切换 15. spring-test-4.1.1.RELEASE.jar 支持Spring组建JUnit和TestNG的单元测试和集成测试。 16. spring-tx-4.1.1.RELEASE.jar 17. spring-web-4.1.1.RELEASE.jar 包含Web应用开发时,用到Spring框架时所需的核心类,包括自动载入WebApplicationContext特性的类、Struts与JSF集成类、文件上传的支持类、Filter类和大量工具辅助类。 18. spring-webmvc-4.1.1.RELEASE.jar 包含SpringMVC框架相关的所有类。包含国际化、标签、Theme、视图展现的FreeMarker、JasperReports、Tiles、Velocity、XSLT相关类。当然,如果你的应用使用了独立的MVC框架,则无需这个JAR文件里的任何类。 19. spring-webmvc-portlet-4.1.1.RELEASE.jar http://www.cnblogs.com/dyllove98/archive/2013/07/01/3165750.html Porlet工作流程和Servlet的主要差异在于,Portlet的请求处理有两个独特 的阶段:动作阶段和显示阶段。动作阶段会有“后台”数据改变或动作的代码,这些代码 只会执行一次。显示阶段会产生用户每次刷新时的看到的显示内容。重要的是, 在单个请求的整个处理过程中,动作阶段只会

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值