[JavaEE] Injecting Bean
So what is a Bean, in JavaEE, any class expect Entity are Bean.
One usefully thing in Bean is Dependency injection. Just like Angular, it is just a Class.
package com.pluralsight.bookstore.utils;
public class TextUtil {
public String sanitize(String textToSanitize) {
return textToSanitize.replaceAll("\\s+", " ");
}
}
Using TextUtil class:
@Transactional(SUPPORTS)
public class BookRepository { // ======================================
// = Injection Points =
// ====================================== @Inject
private TextUtil textUtil; // For creating and deleting methods, we want to use REQUIRED
@Transactional(REQUIRED)
public Book create(@NotNull Book book) {
book.setTitle(textUtil.sanitize(book.getTitle()));
em.persist(book);
return book;
} }
TextUtil is implementation. For DI, we can not only inject an implementation class, we can also inject an Interface.
package com.pluralsight.bookstore.utils;
public interface NumberGenerator {
String generateNumber();
}
But just have interface with implementation, code won't work, we still need to implement generateNumber().
package com.pluralsight.bookstore.utils;
import java.util.Random;
public class IsbnGenerator implements NumberGenerator{
@Override
public String generateNumber() {
return "13-843546-" + Math.abs(new Random().nextInt())
}
}
Using Injection:
@Transactional(SUPPORTS)
public class BookRepository { // ======================================
// = Injection Points =
// ====================================== @Inject
private NumberGenerator generator; @Transactional(REQUIRED)
public Book create(@NotNull Book book) {
book.setIsbn(generator.generateNumber());
book.setTitle(textUtil.sanitize(book.getTitle()));
em.persist(book);
return book;
}
}
Still to make Injection works, we need bean.xml file:
webapp/WEB-INF/beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
The reason why create NumberGenerator as an interface and IsbnGenerator to implement NumberGenerator:
Because we can generate different kinds of number format. IsbnGenerator is one of them.
In order to follow Dependency Inversion principle & Open and Closed principle, we want to create an abstract interface (numberGenerator) with multi different implementations (isbngenerator ...).
[JavaEE] Injecting Bean的更多相关文章
- 菜鸟-手把手教你把Acegi应用到实际项目中(5)
在实际企业应用中,用户密码一般都会进行加密处理,这样才能使企业应用更加安全.既然密码的加密如此之重要,那么Acegi(Spring Security)作为成熟的安全框架,当然也我们提供了相应的处理方式 ...
- [转载]Spring Java Based Configuration
@Configuration & @Bean Annotations Annotating a class with the @Configuration indicates that the ...
- [转] Spring - Java Based Configuration
PS: Spring boot注解,Configuration是生成一个config对象,@Bean指定对应的函数返回的是Bean对象,相当于XML定义,ConfigurationProperties ...
- Java Web整合开发(20) -- Hibernate入门
Spring与Hibernate整合
- 学习Acegi应用到实际项目中(5)
实际企业应用中,用户密码一般都会进行加密处理,这样才能使企业应用更加安全.既然密码的加密如此之重要,那么Acegi(Spring Security)作为成熟的安全框架,当然也我们提供了相应的处理方式. ...
- Grails边做边学入门篇[1]--------大家一起来动手建立project和Domain
近期工作比較忙,没空写博客了.我发现每周五的下午都是我最放松的时候,可能是迟延症的缘故吧...总是寄希望于周末,慢慢的.我的周末就被工作占领了. 希望大家不要有这种坏毛病.今日事,今日毕.当然我们程序 ...
- JavaEE(19) - Web层和EJB的整合(Session Bean)
1. 通过依赖注入访问无状态Session Bean #1. EJB注入Servlet中 #2. EJB注入JSF中 2. 通过EJB引用访问有状态Session Bean 3. 在工具类中访问Ses ...
- JavaEE开发之Spring中Bean的作用域、Init和Destroy方法以及Spring-EL表达式
上篇博客我们聊了<JavaEE开发之Spring中的依赖注入以及AOP>,本篇博客我们就来聊一下Spring框架中的Bean的作用域以及Bean的Init和Destroy方法,然后在聊一下 ...
- JavaEE Tutorials (7) - 在会话bean中使用异步方法调用
7.1异步方法调用88 7.1.1创建异步业务方法88 7.1.2从企业bean客户端调用异步方法897.2async示例应用90 7.2.1async—war模块的架构91 7.2.2运行async ...
随机推荐
- PHP安装yaf在ubuntu下面的问题解决
1.在执行make的时候出现如下错误: In file included from /root/yaf-2.1.2/yaf_router.c:28: /usr/include/php/ext/pcre ...
- 第2章 JavaScript语法
1.最好的做法是把<script>标签放到html文档的最后,</body>标签之前. 举例: ...... <script src="file.js" ...
- UNICODE本地编译freescale的i.MX6Q的android4.2.2&android4.4.2 && 全志a80的步骤x1
20151031本地编译freescale的i.MX6Q的android4.2.2&android4.4.2 && 全志a80的步骤x1 2015/10/31 15:07 开始 ...
- MySql备份表数据
一:根据user表创建user_backup表 drop table if exists user_backup; create table user_backup like user; // lik ...
- centos如何离线安装部署node&pm2?
最近我们项目要上即时通讯,因为项目对安全要求比较高,所以选择了即时通讯云服务器yun2win,他们提供了数据服务器让我们自己安装部署.那么问题来了,我们服务器是放在内网,完全无法访问外网,而yun2w ...
- 第五届蓝桥杯校内选拔第六题_(dfs)
你一定听说过“数独”游戏.如[图1.png],玩家需要根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个同色九宫内的数字均含1-9,不重复. 数独的答案都是唯一的,所以 ...
- DWARF调试格式的简介
DWARF调试格式的简介 Michael J. Eager, Eager Consulting Feb, 2007 翻译:吴晖 2012年2月 如果我们可以编写确保能正确工作且永远不需要调试的程序,这 ...
- Masonry 原理一
Under the hood Auto Layout is a powerful and flexible way of organising and laying out your views. H ...
- MFC_2.8 使用状态栏工具栏
使用状态栏工具栏 1.资源-添加-TOOLBAR 画图标.画了一个,第二个会出来. 2.头文件添加成员 CToolBar m_ToolBar; CStatusBar m_StatusBar; 3.初始 ...
- CAD绘制一个图象标记对象(com接口VB语言)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...