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的更多相关文章

  1. 菜鸟-手把手教你把Acegi应用到实际项目中(5)

    在实际企业应用中,用户密码一般都会进行加密处理,这样才能使企业应用更加安全.既然密码的加密如此之重要,那么Acegi(Spring Security)作为成熟的安全框架,当然也我们提供了相应的处理方式 ...

  2. [转载]Spring Java Based Configuration

    @Configuration & @Bean Annotations Annotating a class with the @Configuration indicates that the ...

  3. [转] Spring - Java Based Configuration

    PS: Spring boot注解,Configuration是生成一个config对象,@Bean指定对应的函数返回的是Bean对象,相当于XML定义,ConfigurationProperties ...

  4. Java Web整合开发(20) -- Hibernate入门

    Spring与Hibernate整合

  5. 学习Acegi应用到实际项目中(5)

    实际企业应用中,用户密码一般都会进行加密处理,这样才能使企业应用更加安全.既然密码的加密如此之重要,那么Acegi(Spring Security)作为成熟的安全框架,当然也我们提供了相应的处理方式. ...

  6. Grails边做边学入门篇[1]--------大家一起来动手建立project和Domain

    近期工作比較忙,没空写博客了.我发现每周五的下午都是我最放松的时候,可能是迟延症的缘故吧...总是寄希望于周末,慢慢的.我的周末就被工作占领了. 希望大家不要有这种坏毛病.今日事,今日毕.当然我们程序 ...

  7. JavaEE(19) - Web层和EJB的整合(Session Bean)

    1. 通过依赖注入访问无状态Session Bean #1. EJB注入Servlet中 #2. EJB注入JSF中 2. 通过EJB引用访问有状态Session Bean 3. 在工具类中访问Ses ...

  8. JavaEE开发之Spring中Bean的作用域、Init和Destroy方法以及Spring-EL表达式

    上篇博客我们聊了<JavaEE开发之Spring中的依赖注入以及AOP>,本篇博客我们就来聊一下Spring框架中的Bean的作用域以及Bean的Init和Destroy方法,然后在聊一下 ...

  9. 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 ...

随机推荐

  1. 网站开发综合技术 HTML

    HTML            内容(Hyper Text Markup Language,超文本标记语言) CSS             网页美化 Javascript      脚本语言 第一部 ...

  2. php redis 操作大全

    类和方法 用法 Redis类 类RedisException 预定义的常量 Redis类 说明:创建一个Redis客户端 例 $redis = new Redis(); 类RedisException ...

  3. win7 中使用NFS共享

    转自和修改自:http://blog.sina.com.cn/s/blog_553761ef0100oevm.html 一 安装 在卸载或更改程序->打开或关闭windows的功能-> 安 ...

  4. DVWA--登录页面错误问题 469 | | PHP Fatal error: Uncaught exception 'PDOException' with message 'could not find driver' in C:\web\DVWA\dvwa\includes\dvwaPage.inc.php:469

    // MySQL PDO Prepared Statements (for impossible levels) $db = new PDO('mysql:host=' . $_DVWA[ 'db_s ...

  5. 编写Java脚本统计工程代码总行数

    在新公司工作将近一年了,一直独自一人负责服务端集群的运维和代码的编写.不知不觉从一个Project发展到了七八个Project. 看着越来越多的代码,今天突然想统计一下一共写了多少代码.[这里只统计完 ...

  6. 00-IT人士必去的10个网站

    IT人士必去的10个网站 1.Chinaunix 网址:http://www.chinaunix.net/ 简介:中国最大的linux/unix技术社区. 2.ITPub 网址:http://www. ...

  7. nfs服务权限配置

    nfs服务权限配置 1. 查看系统是否已经安装了服务Rpm -qa | grep nfs 2. 启动服务,并且开机自动运行Systemctl start nfsSystemctl enabled nf ...

  8. poj3061 Subsequence【尺取法】

    Description A sequence of N positive integers (10 < N < 100 000), each of them less than or eq ...

  9. linux less-分屏上下翻页浏览文件内容

    博主推荐:获取更多 linux文件内容查看命令 收藏:linux命令大全 less命令的作用与more十分相似,都可以用来浏览文字档案的内容,不同的是less命令允许用户向前或向后浏览文件,而more ...

  10. JDBC在Java Web中的应用

    JDBC在Java Web中的应用 制作人:全心全意 在Java Web开发中,JDBC的应用十分广泛.通常情况下,Web程序操作数据库都是通过JDBC实现,即使目前数据库方面的开源框架层出不穷,但其 ...