- 浏览: 994299 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (1441)
- 软件思想&演讲 (9)
- 行业常识 (250)
- 时时疑问 (5)
- java/guava/python/php/ruby/R/scala/groovy (213)
- struct/spring/springmvc (37)
- mybatis/hibernate/JPA (10)
- mysql/oracle/sqlserver/db2/mongdb/redis/neo4j/GreenPlum/Teradata/hsqldb/Derby/sakila (268)
- js/jquery/jqueryUi/jqueryEaseyUI/extjs/angulrJs/react/es6/grunt/zepto/raphael (81)
- ZMQ/RabbitMQ/ActiveMQ/JMS/kafka (17)
- lucene/solr/nuth/elasticsearch/MG4J (167)
- html/css/ionic/nodejs/bootstrap (19)
- Linux/shell/centos (56)
- cvs/svn/git/sourceTree/gradle/ant/maven/mantis/docker/Kubernetes (26)
- sonatype nexus (1)
- tomcat/jetty/netty/jboss (9)
- 工具 (17)
- ETL/SPASS/MATLAB/RapidMiner/weka/kettle/DataX/Kylin (11)
- hadoop/spark/Hbase/Hive/pig/Zookeeper/HAWQ/cloudera/Impala/Oozie (190)
- ios/swift/android (9)
- 机器学习&算法&大数据 (18)
- Mesos是Apache下的开源分布式资源管理框架 (1)
- echarts/d3/highCharts/tableau (1)
- 行业技能图谱 (1)
- 大数据可视化 (2)
- tornado/ansible/twisted (2)
- Nagios/Cacti/Zabbix (0)
- eclipse/intellijIDEA/webstorm (5)
- cvs/svn/git/sourceTree/gradle/jira/bitbucket (4)
- jsp/jsf/flex/ZKoss (0)
- 测试技术 (2)
- splunk/flunm (2)
- 高并发/大数据量 (1)
- freemarker/vector/thymeleaf (1)
- docker/Kubernetes (2)
- dubbo/ESB/dubboX/wso2 (2)
最新评论
下面是一个典型的Spring配置文件(application-config.xml)
Xml代码 收藏代码
<beans>
<bean id="orderService" class="com.acme.OrderService"/>
<constructor-arg ref="orderRepository"/>
</bean>
<bean id="orderRepository" class="com.acme.OrderRepository"/>
<constructor-arg ref="dataSource"/>
</bean>
</beans>
然后你就可以像这样来使用是bean了:
Java代码 收藏代码
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");
OrderService orderService = (OrderService) ctx.getBean("orderService");
现在Spring Java Configuration这个项目提供了一种通过java代码来装配bean的方案:
Java代码 收藏代码
@Configuration
public class ApplicationConfig {
public @Bean OrderService orderService() {
return new OrderService(orderRepository());
}
public @Bean OrderRepository orderRepository() {
return new OrderRepository(dataSource());
}
public @Bean DataSource dataSource() {
// instantiate and return an new DataSource …
}
}
然后你就可以像这样来使用是bean了:
Java代码 收藏代码
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);
OrderService orderService = ctx.getBean(OrderService.class);
这么做有什么好处呢?
1.使用纯java代码,不在需要xml
2.在配置中也可享受OO带来的好处
3.类型安全对重构也能提供良好的支持
4.依旧能享受到所有springIoC容器提供的功能
Xml代码 收藏代码
<beans>
<bean id="orderService" class="com.acme.OrderService"/>
<constructor-arg ref="orderRepository"/>
</bean>
<bean id="orderRepository" class="com.acme.OrderRepository"/>
<constructor-arg ref="dataSource"/>
</bean>
</beans>
然后你就可以像这样来使用是bean了:
Java代码 收藏代码
ApplicationContext ctx = new ClassPathXmlApplicationContext("application-config.xml");
OrderService orderService = (OrderService) ctx.getBean("orderService");
现在Spring Java Configuration这个项目提供了一种通过java代码来装配bean的方案:
Java代码 收藏代码
@Configuration
public class ApplicationConfig {
public @Bean OrderService orderService() {
return new OrderService(orderRepository());
}
public @Bean OrderRepository orderRepository() {
return new OrderRepository(dataSource());
}
public @Bean DataSource dataSource() {
// instantiate and return an new DataSource …
}
}
然后你就可以像这样来使用是bean了:
Java代码 收藏代码
JavaConfigApplicationContext ctx = new JavaConfigApplicationContext(ApplicationConfig.class);
OrderService orderService = ctx.getBean(OrderService.class);
这么做有什么好处呢?
1.使用纯java代码,不在需要xml
2.在配置中也可享受OO带来的好处
3.类型安全对重构也能提供良好的支持
4.依旧能享受到所有springIoC容器提供的功能
发表评论
-
Spring Boot
2017-09-26 09:51 252Spring Boot是由Pivotal团 ... -
spring AOP中切点
2017-08-25 09:59 861在spring AOP中,需要使用AspectJ的切点表达式 ... -
Spring JdbcTemplate详解
2017-07-19 16:12 557JdbcTemplate简介 Spring对数据库的操 ... -
Spring相关
2017-04-20 16:10 429我是一个Java开发者,之前知道Spring属于这个公司,就 ... -
spring.schemas、spring.handlers的使用
2017-02-28 13:55 1678报错信息:Configuration problem: Un ... -
Spring的一个命名空间的名称空间处理程序没有找到
2017-02-25 15:20 9051. 问题 本文将讨论Spring中最常见的配置问题 —— ... -
到底EJB是什么
2016-12-06 10:05 420到底EJB是什么?被口口相传的神神秘秘的,百度一番,总觉得 ... -
Spring MVC 和 Servlet 一样,都不是线程安全的
2016-04-28 01:06 628你的理解是对的,Spring MVC 和 Servlet 一 ... -
springmvc的control 的线程是否安全的问题
2016-05-31 10:09 324关于java Servlet,Struts,springM ... -
框架的一些学习
2016-02-03 14:53 222java aopalliance-1.0.jar这个包是AOP ... -
Metrics介绍和Spring的集成
2016-02-02 16:56 904Metrics可以为你的代码的运行提供无与伦 ... -
spring mvc常用的注解
2016-01-22 14:28 664spring mvc常用的注解: ... -
Spring的常用注解
2016-01-20 16:07 612Spring2.5引入注解式处理器 @Controlle ... -
通过Spring的配置,添加多个数据源,制作多个qlMapClient,iBatis或Hibernate的各个DAO
2016-12-27 10:25 478通过Spring的配置,添加多个数据源,制作多个qlMapCl ... -
springmvc避免IE执行AJAX时,返回JSON出现下载文件
2017-01-01 23:35 823<!-- 避免IE执行AJAX时,返回JSON出现下载文 ... -
springMVC的@ResponseBody出现乱码解决方法
2017-01-02 00:23 346使用@ResponseBody出现乱码解决方法 1、 Re ... -
Spring中的Bean的注入方式
2017-01-02 00:27 502一 setter方法注入 配置文件如下: <bean ... -
spring发送邮件配置文件
2017-01-02 00:27 12381、发送邮件配置文件springmail_config ... -
@Resource和@Autowire的区别
2017-01-02 00:27 663@Resource和@Autowire的区别 在java代码中 ... -
Spring中继承并简化了Quartz
2017-01-02 00:27 635Quartz是一个强大的企业级任务调度框架,Spring中继承 ...
相关推荐
普通的POJO只要标注@Configuration注解,就可以为Spring容器提供Bean定义的信息了,每个标注了@Bean的类方法都相当于提供一个Bean的定义信息。 基于Java类的配置方法和基于XML或基于注解的配置方式相比,前者通过...
在此基础上,又提供了spi机制,用spring.factories可以完成一个小组件的自动装配功能。 在一般业务场景,可能你不大关心一个bean是如何被注册进spring容器的。只需要把需要注册进容器的bean声明为@Component即可,...
使用 Java 代码可以检查类型安全。 @EnableAutoConfiguration 自动配置。 @ComponentScan 组件扫描,可自动发现和装配一些 Bean。 @Component 可配合 CommandLineRunner 使用,在程序启动后执行一些基础任务。 @...
9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...
9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring JDBC包结构...
使用@ModelAttribute提供一个从模型到数据的链接 13.12.6. 使用@SessionAttributes指定存储在会话中的属性 13.12.7. 自定义WebDataBinder初始化 13.13. 更多资源 14. 集成视图技术 14.1. 简介 14.2. JSP和...
9.9.1. 对一个特定的 DataSource 使用错误的事务管理器 9.10. 更多的资源 10. DAO支持 10.1. 简介 10.2. 一致的异常层次 10.3. 一致的DAO支持抽象类 11. 使用JDBC进行数据访问 11.1. 简介 11.1.1. Spring ...
使用@ModelAttribute提供一个从模型到数据的链接 13.12.6. 使用@SessionAttributes指定存储在会话中的属性 13.12.7. 自定义WebDataBinder初始化 13.13. 更多资源 14. 集成视图技术 14.1. 简介 14.2. JSP和...
2.4项目实战——第一个javaee应用:helloworld 28 2.4.1开始创建第一个javaee应用 28 2.4.2熟悉helloworld框架结构 29 本章小结 32 课后练习 32 第3章jsp和servlet 33 3.1开发第一个jsp+servlet应用 33 3.1.1...
@Configuration注解会告诉Spring这是一个配置类,会为Spring某些提供bean。 自动配置起源于自动装配(Autowiring)和组件扫描(component scan) 可以通过组件扫描,Spring可以发现路径下的组件,并创建成一体的
从Spring3.0,@ Configuration用于定义的配置类,可替换XML配置文件,被注解的类内部包含一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext...
1. bean的装配方式 1.表明当前类是一个配置类,是方法bean的源 2.将@Configuration配置的AppConfig的BeanDefinitio
第一部分 Spring framework概述.......................................................................................................................6 第1章 Spring Framework介绍...........................
一. Java基础 5 (一). Java运行环境 5 1.eclipse的安装 5 ...6. 指定Bean引用与自动装配 276 (二) AOP 278 1. AOP简介 278 (三) Spring 3.X 新特性(JavaConfig,SpEL)简介 285 1. JavaConfig 285 2. SpEL 287
1. 利用注解、反射和工厂模式设计一个简单的IoC容器 2. 该IoC容器包含3个注解和一个IoC容器类(AnnotationConfigApplicationContext),其定义如下: 注解 含义 @Component 标注Bean @Autowired 标注需要被注入的...
概述Mockitobeans是一个 ,通过使用Spring自己的@Autowire语法,您可以无缝地对所有bean进行自动装配。历史mockitobeans时,我一直在寻找一种方式来开始使用嘲笑说已经自动装配依赖其中有额外的自动装配Autowired的...
一、注解(annotations)列表 ...组件扫描,可自动发现和装配一些Bean。 3、@Configuration 等同于Spring的XML配置文件;使用Java代码可以检查类型安全。 4、@EnableAutoConfiguration 自动配置 5、@RestContro
如果需要实现自己的配置管理器,开发者则可以实现一个实现Configuration接口的类,该类可以自己加载Struts 2配置文件. struts.locale 指定Web应用的默认Locale. struts.i18n.encoding 指定Web应用的默认编码集.该...
• 扩展性 —— Webx 3.0对Spring做了扩展,使Spring Bean不再是“bean”,而是升级成“组件”。一个组件可以扩展另一个组件,也可以被其它组件扩展。这种机制造就了Webx的非常好的扩展性,且比未经扩展的Spring更易...