Spring中IoC - 两种ApplicationContext加载Bean的配置
说明:Spring IoC其实就是在Service的实现中定义了一些以来的策略类,这些策略类不是通过 初始化、Setter、工厂方法来确定的。而是通过一个叫做上下文的(ApplicationContext)组建来加载进来的。这里介绍两种Context组建的构件过程
前提条件:在Gradle工程的build.gradle文件中引入对Spring framework 的支持
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.springframework',name: 'spring-context', version: '4.1.5.RELEASE'
compile group: 'org.springframework',name: 'spring-core', version: '4.1.5.RELEASE'
testCompile group: 'junit', name: 'junit', version: '4.11'
}
第一种方式:通过ClassPathXmlApplicationContext加载xml配置文件
这里使用greenmail做测试邮件的例子来介绍:
1. 依赖管理
<?xml version="1.0" encoding="UTF-8"?>
<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.ygshen.mvnbook.account</groupId>
<artifactId>account-service</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.3.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.3.3.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.3.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.3.RELEASE</version>
</dependency> <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/com.icegreen/greenmail -->
<dependency>
<groupId>com.icegreen</groupId>
<artifactId>greenmail</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. 接口定义
public interface emailService {
public void sendMail(String to, String subject,String htmlText) throws MessagingException;
}
3. 接口实现
public class emailServiceImp implements emailService{
//这里有两个IOC的变量,后面将通过bean+property的方式进行依赖管理
private JavaMailSender javaMailSender;
private String systemAccount;
// Get和Set时必须的
public JavaMailSender getJavaMailSender() {
return javaMailSender;
}
public void setJavaMailSender(JavaMailSender javaMailSender) {
this.javaMailSender = javaMailSender;
}
public String getSystemAccount() {
return systemAccount;
}
public void setSystemAccount(String systemAccount) {
this.systemAccount = systemAccount;
}
//发送邮件主题函数,javamailsender是 spring的邮件类。这个mail sender就是发送邮件的smtp服务器。
// 如Gmail等,这里将在测试用例中使用greenemail代替他门
public void sendMail(String to, String subject, String htmlText) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message);
helper.setFrom(systemAccount);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(htmlText);
javaMailSender.send(message);
}
}
4. Bean文件定义:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--定义自动获取property配置-->
<bean id="propertyConfigure" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:service.properties"></property>
</bean>
<!--定义java mail sender bean 对象,这个bean后边将作为依赖注入到service中-->
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="protocol" value="${email.protocol}"></property>
<property name="port" value="${email.port}"></property>
<property name="host" value="${email.host}"></property>
<property name="username" value="${email.username}"></property>
<property name="password" value="${email.password}"></property>
<property name="javaMailProperties">
<props>
<prop key="mail.${email.protocol}.auth">${email.auth}</prop>
</props>
</property> </bean>
<!--真正负责发送邮件的类定义bean-->
<bean id="accountService" class="com.ygshen.mvnbook.account.email.emailServiceImp">
<property name="javaMailSender" ref="javaMailSender"></property>
<property name="systemAccount" value="${email.systemAccount}"></property>
</bean>
</beans>
5. Property 文件
email.protocol=smtps
email.host=smtp.gmail.com
email.port=465
email.username=shenyuangong@gmail.com
email.password=111111
email.auth=true
email.systemAccount=ygshen@163.com
初始化javamailsender的配置文件。 main/Resources目录下。注意这个配置文件将在test/resource中 重新定义目的是模拟测试
6. 测试代码:
package com.ygshen.mvnbook.account; import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.ServerSetup;
import com.ygshen.mvnbook.account.email.emailService;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.mail.Message;
import javax.mail.MessagingException; /**
* Created by ygshen on 16/10/31.
*/
//这里需要注意junit的测试文件必须以Test结尾 否则 mvn clean test 的时候将没有办法获取到测试用例
public class emailServiceTest {
//这里初始化一个本地的greenmail作为测试邮箱服务器
private GreenMail mail;
@Before
public void startMailBox(){
mail = new GreenMail(new ServerSetup(12000,null,"smtp"));
mail.setUser("shenyuangong@gmail.com", "123456");
mail.start();
}
@Test
public void sendMail() throws MessagingException {
ApplicationContext context = new ClassPathXmlApplicationContext("account-service.xml");
// 这里将初始化发送邮件的service,而service所依赖的javamailsender初始化所配置的邮箱服务器和start中 定义的port、protocol也是一样的。 在测试test/resources中定义。这里greenmail起到模拟gmail服务器的能力
emailService service = (emailService)context.getBean("accountService");
service.sendMail("test2@163.com","Test Subject","<h1>test</h1>");
mail.waitForIncomingEmail(2000,1);
Message[] msgs = mail.getReceivedMessages();
Assert.assertEquals(1,msgs.length);
}
@After
public void closeMail(){
mail.stop();
} }
7. 测试用的Properties文件
email.protocol=smtp
email.host=localhost
email.port=12000
# 注意这里的邮箱登陆的帐号要和Greenmail中 setUser使用的相同才行。
email.username=shenyuangong@gmail.com
email.password=123456
email.auth=true
email.systemAccount=ygshen@163.com
第二种方式:AnnotationConfigApplicationContext类加载JavaClass File的配置文件
配置文件JavaClass如下:
@Configuration:表示这是一个配置文件
@Bean: 表示这是一个Bean(名字为方法名),将来他要用来与Service中的@Autowired属性配对,注意配对的时候是根据返回类型来对应的,也就是说所有的Service中但凡有@Autowired的属性,他们都是从这个配置文件中拿到的。
@Configuration
public class ApplicationContextConfiguration {
@Bean
public AccountRepoisitory accountRepoisitory() {
return new AccountRepositoryImp();
} @Bean
public TransactionService transactionService() {
return new TransactionServiceImp();
} @Bean
public TransferService transferService() {
return new TransferServiceImp();
}
}
再来看一下使用@AutoWired的Service类。这个AutoWired将与上面配置文件中的@Bean结成一对儿
public class TransactionServiceImp implements TransactionService {
@Autowired
public AccountRepoisitory accountRepoisitory;
@Override
public void NewTransaction(String accountId1, String accountId2, double money) {
Account account1=accountRepoisitory.GetAccountByAccountId(accountId1);
Account account2=accountRepoisitory.GetAccountByAccountId(accountId2);
Transaction transaction=new Transaction(){ };
transaction.fromAccount=account1;
transaction.toAccount=account2;
transaction.moneyTransfered=money;
transaction.transactionDate= Calendar.getInstance().getTime();
BankFactory.Transactions.add(transaction);
}
}
最后来看Main函数是如何将配置文件与Service文件结合在一起的。 很简单
ApplicationContext context=
new AnnotationConfigApplicationContext(
com.ctrip.configuration.ApplicationContextConfiguration.class
); // 接下来我们就可以使用任何Service中定义的方法了
AccountRepoisitory accountRepoisitory=context.getBean("accountRepoisitory",
AccountRepositoryImp.class);
TransactionService transactionService=context.getBean("transactionService",
TransactionServiceImp.class);
TransferService transferService=context.getBean("transferService",
TransferServiceImp.class
);
transferService.Transfer("1","2",234);
Spring中IoC - 两种ApplicationContext加载Bean的配置的更多相关文章
- 两种动态加载JavaScript文件的方法
两种动态加载JavaScript文件的方法 第一种便是利用ajax方式,第二种是,动静创建一个script标签,配置其src属性,经过把script标签拔出到页面head来加载js,感乐趣的网友可以看 ...
- Spring中ApplicationContext加载机制和配置初始化
Spring中ApplicationContext加载机制. 加载器目前有两种选择:ContextLoaderListener和ContextLoaderServlet. ...
- 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
该系列文章是本人在学习 Spring 的过程中总结下来的,里面涉及到相关源码,可能对读者不太友好,请结合我的源码注释 Spring 源码分析 GitHub 地址 进行阅读 Spring 版本:5.1. ...
- Spring中使用两种Aware接口自定义获取bean
在使用spring编程时,常常会遇到想根据bean的名称来获取相应的bean对象,这时候,就可以通过实现BeanFactoryAware来满足需求,代码很简单: @Servicepublic clas ...
- iOS两种方式加载图片的区别
加载图片的方式: imageNamed: imageWithContentsOfFile: 加载Assets.xcassets这里面的图片: 1> 打包后变成Assets.car 2> 拿 ...
- 【Spring源码分析系列】加载Bean
/** * Create a new XmlBeanFactory with the given input stream, * which must be parsable using DOM. * ...
- Spring在Web项目中的三种启动加载的配置
在最近的项目中,使用到了spring相关的很多东西,有点把spring的配置给搞混了,从网上查到的资料以及整理了一下. 在Web项目中,启动spring容器的方式有三种,ContextLoaderLi ...
- MyEclipse10 中的两种FreeMarker插件的安装与配置
第一个插件是: freemarker-ide MyEclipce10.0中安装FreeMarker插件,这绝对是最简单的方法 ...
- 深入理解 spring 容器,源码分析加载过程
Spring框架提供了构建Web应用程序的全功能MVC模块,叫Spring MVC,通过Spring Core+Spring MVC即可搭建一套稳定的Java Web项目.本文通过Spring MVC ...
随机推荐
- linux软件安装(rpm,源码编译)
1.rpm(redhat package manager)管理器主要目的在于解决软件的安装.卸载.升级.查询.验证等,例如升级过程中,保留软件的配置文件,安装过程中,检查软件依赖的库文件,以及卸载过程 ...
- iscsi模型相关点
1.iscsi即ip scsi,按架构分为控制器架构.链接桥系统架构.pc系统架构.这里主要说明一下pc系统架构. 所谓的pc系统架构,就是利用target软件存储端+initiator客户端+tcp ...
- [XMPP]简易的聊天室实现[二](使用CocoaAsyncSocket)
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- cxf2.7.10 与 spring3.0.5集成
开发环境: NetBeans7.4 Tomcat 6.0.32 一 服务端: 1:新建JavaWeb工程 cxfspring-server,导入jar包如下图所示: 2:在web.xml文件中添加如下 ...
- java设计模式--行为型模式--备忘录模式
备忘录模式,我们平常所做的备忘录么.还得深深研究哦. 备忘录模式: 备忘录模式 概述 在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可将该对象恢复到原先保存的状 ...
- [C# 基础知识系列]专题九: 深入理解泛型可变性
引言: 在C# 2.0中泛型并不支持可变性的(可变性指的就是协变性和逆变性),我们知道在面向对象的继承中就具有可变性,当方法声明返回类型为Stream,我们可以在实现中返回一个FileStream的类 ...
- hdu 5625 Clarke and chemistry
Problem Description Clarke is a patient with multiple personality disorder. One day, Clarke turned i ...
- 让magento的validate验证hidden field
Object.extend(Validation, { isVisible : function(elm) { return true; }, insertAdvice : function(elm, ...
- AC自动机跟随Kuangbing学习笔记
http://www.cnblogs.com/kuangbin/p/3164106.html kuangbin的博客 第一段代码基本是COPY kuangbin的.. 1.HDU 2222 Keywo ...
- hive大数据倾斜总结
在做Shuffle阶段的优化过程中,遇到了数据倾斜的问题,造成了对一些情况下优化效果不明显.主要是因为在Job完成后的所得到的 Counters是整个Job的总和,优化是基于这些Counters得出的 ...