领先一步
VMware提供培训和认证,以加速您的进步。
了解更多在Spring框架中,许多技术特性依赖于代理的使用。我们将通过三个例子深入探讨这个主题:事务、缓存和Java配置。
本博客文章中显示的所有代码示例都可以在我的github账户上找到。
@Service
public class AccountServiceImpl implements AccountService {
//…
//Not specifying a transaction policy here!
public void create(Account account) {
entityManager.persist(account);
}
}
由于“create”方法不是事务性的,它很可能会抛出异常(因为Account对象不应该在事务之外持久化)。
@Service
public class AccountServiceImpl implements AccountService {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public void create(Account account) {
entityManager.persist(account);
}
}
这是相应的Spring配置
<bean id="transactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<tx:annotation-driven/>
在Spring通用配置中,我们使用了 <tx:annotation-driven />。这意味着所有@Transactional注解都应该在启动时被扫描,并且目标方法应该成为事务性的。那么事务行为在哪里发生呢?
启动之前,我们仍然与之前一样。
在启动时,会创建一个名为代理的新类。它负责添加事务行为,如下所示:
生成的代理类位于AccountServiceImpl之上。它向其中添加了事务行为[1]。
那么如何确保确实使用了代理呢?为了更好地理解,您可以回到代码中,亲眼看看您是否确实使用了代理。
一个简单的方法是打印出类名。
AccountService accountService = (AccountService) applicationContext.getBean(AccountService.class);
String accountServiceClassName = accountService.getClass().getName();
logger.info(accountServiceClassName);
在我的电脑上,它显示以下输出:
INFO : transaction.TransactionProxyTest - $Proxy13
这个类是一个动态代理,由Spring使用JDK反射API生成(更多信息请点击此处)。
在关闭时(例如,当应用程序停止时),代理类将被销毁,并且您在文件系统上只有AccountService和AccountServiceImpl。
@Controller public class AccountController { private AccountService accountService;
private void setAccountService(AccountService accountService) { this.accountService=accountService; }
//… }
<a href="http://blog.springsource.org/wp-content/uploads/2012/05/proxy-and-target1.png"><img class="aligncenter size-full wp-image-11128" title="proxy-and-target" src="http://blog.springsource.org/wp-content/uploads/2012/05/proxy-and-target1.png" alt="" width="316" height="255" /></a>
</div>
<div>
The attribute accountService is of type AccountService (interface). The variable dependency is on the interface type AccountService, not the implementation type, which reduces the coupling between classes. This is a best practice.
As seen before, both AccountServiceImpl and the generated Proxy implement the interface AccountService.
• If there is a proxy, Spring injects the proxy
• If not, Spring injects the instance of type AccountServiceImpl.
</div>
<h3><a name="cache">Caching</a></h3>
<div>
Declarative caching is a new feature in Spring 3.1 that works like Spring’s declarative transaction support.
The @Cacheable annotation should be used in that way:
</div>
<div>
```java
public class AccountServiceImpl implements AccountService {
@Cacheable(value="accounts", key="#id")
public Account findAccount (long id) {
// only enter method body if result is not in the cache already
}
}
您还应该在Spring配置中启用缓存,如下所示:
<cache:annotation-driven />
以下是预期结果:
accountService.findAccount (1); // Result stored into cache for key “1”
accountService.findAccount (1); // Result retrieved from cache. Target method not called.
accountService.findAccount (2); // Result stored into cache for key “2”
运行时,使用代理来添加缓存行为。
默认情况下,如果您的bean没有实现接口,Spring使用技术继承:在启动时,会创建一个新类。它继承自您的bean类,并在子方法中添加行为。
注意:本节需要您具备Spring Java配置的一些背景知识。如果您不熟悉这种新的配置样式,可以随意跳过本节。
@Configuration public class JavaConfig {
@Bean public AccountService accountService() {
return new AccountServiceImpl((accountRepository()); } @Bean public AccountRepository accountRepository () { //… }
}
Spring calls the method accountService() every time it needs to wire an instance of the bean “accountService” and this one returns a “new” object of type AccountService. If 10 beans are using a dependency of type AccountService, this method is called 10 times.
However, no matters the Spring configuration has been made using Java Configuration or not, every bean should be a singleton by default. How is that possible and where is the magic happening?
This diagram explains how things work internally:
</div>
<div><a href="http://blog.springsource.org/wp-content/uploads/2012/05/java-config.png"><img class="aligncenter size-full wp-image-11131" title="java-config" src="http://blog.springsource.org/wp-content/uploads/2012/05/java-config.png" alt="" width="507" height="328" /></a></div>
<div>
So the Proxy is adding behavior there. In the case that your bean should be a singleton, the action to turn your Plain Old Java Object into a singleton is performed by a child class (Proxy).
</div>
<div>
<h3>Conclusion</h3>
We’ve seen some use-cases on how proxies are used inside the Spring framework. There are many other examples: Aspect Oriented Programming, Security (using Spring Security), thread safety, scopes, etc…
If you would like to know more on the impact on performance when using proxies, you can read <a href="http://blog.springsource.org/2007/07/19/debunking-myths-proxies-impact-performance/">Alef Arendsen’s blog entry here</a>.
</div>
<div>
<hr size="1" />
<div><a name="note">[1]</a>to be exact: the proxy class does not contain the transaction code internally. It delegates transaction handling to some classes that are part of the Spring framework. Spring will then handle transactions according to the Transaction Manager you have declared.
</div>
</div>