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. 聊聊MyBatis缓存机制

    https://tech.meituan.com/mybatis_cache.html 前言 MyBatis是常见的Java数据库访问层框架.在日常工作中,开发人员多数情况下是使用MyBatis的默认 ...

  2. Jax

    The scope of this project is to automate the current Credit Correction process of opening, editing, ...

  3. Android4.4以上Uri转换成绝对路径的工具类

    一.Android4.4版本以上Uri地址封装规范: content://com.android.providers.media.documents/document/image%3A659 二.An ...

  4. Hadoop的数据采集框架

    问题导读: Hadoop数据采集框架都有哪些? Hadoop数据采集框架异同及适用场景? Hadoop提供了一个高度容错的分布式存储系统,帮助我们实现集中式的数据分析和数据共享.在日常应用中我们比如要 ...

  5. 揭开WebService的神秘面纱

    一.序言 大家或多或少都听过WebService(Web服务),有一段时间很多计算机期刊.书籍和网站都大肆的提及和宣传WebService技术,其中不乏很多吹嘘和做广告的成分.但是不得不承认的是Web ...

  6. datagrid上面的查询按钮设置了,但是分页工具栏不显示

    原因:查询的linkbutton没有放在toolbar里. <script type="text/javascript"> $(function(){ $('#dg') ...

  7. CF842C Ilya And The Tree

    思路: 1. 如果根节点是0,那么可以通过一次dfs计算出所有节点的最大值. 2. 如果根节点不是0,那么其余各点的最大值一定是根节点的一个因子.首先计算出根节点的所有因子.在dfs到一个深度为d的节 ...

  8. 2199. [HZOI 2016] 活动投票

    ★★   输入文件:hztp.in   输出文件:hztp.out   简单对比 时间限制:0.5 s   内存限制:2 MB [题目描述] 衡中活动很多,人也很多,一次活动有n个学生参与投票,现已知 ...

  9. Windows 2008 防火墙开放端口

    当我们使用新服务器架设新主机时,经常会遇到网站无法访问的情况,当问及客服时,经常会告知,操作系统默认不打开80端口,请先确定80是否打开并确定没有被占用.那么,我们该如何打开80端口呢? 方法/步骤 ...

  10. 生产环境4.3.5jboss内存调优

    1.查看jboss的监控工具 http://XXX/jmx-console/htmladaptor 2.查看jvm的监控工具 jdk\bin jvisualvm.exe jmc.exe 3.查看jbo ...