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. 实现div左右上下都居中

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 2559. [NOIP2016]组合数问题

    [题目描述] [输入格式] 从文件中读入数据. 第一行有两个整数t, k,其中t代表该测试点总共有多少组测试数据,k的意义见[问题描述]. 接下来t行每行两个整数n, m,其中n, m的意义见[问题描 ...

  3. Android 简单的语音播报

    不解释快上车 Main.class package com.example.myapp; import android.app.AlertDialog;import android.os.Bundle ...

  4. 左耳听风 ARTS Week 001

    要求:1.每周至少做一个 leetcode 的算法题 2.阅读并点评至少一篇英文技术文章 3.学习至少一个技术技巧 4.分享一篇有观点和思考的技术文章 1.每周至少做一个 leetcode 的算法题 ...

  5. dwarfdump --arch=arm64 --lookup

    解析友盟错误信息重要指令: dwarfdump --arch=arm64 --lookup 0x1001edbc4 /Users/zhoujunbo/Library/Developer/Xcode/A ...

  6. nz-card头部右侧添加东西

    <nz-card [nzBordered]="true" nzTitle="卡片标题" [nzExtra]="extraTemplate1&qu ...

  7. CAD使用GetXData读数据(com接口)

    主要用到函数说明: MxDrawEntity::GetXData 返回实体的扩展数据. c#代码实现如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 ...

  8. CAD与用户互在图面上得到一个矩形框(com接口VB语言)

    主要用到函数说明: MxDrawXCustomFunction::ExApp_CutDwg 与用户互在图面上得到一个矩形框,详细说明如下: 参数 说明 IN DOUBLE dX1 保存范围的左下角位置 ...

  9. python安装外部模块Django

    Windows安装Django模块: 由于本人安装的Python版本是Python3.7,所以安装命令为:pip3 install django /pip3 install django安装过程中出现 ...

  10. Jquery 上一步、下一步及提交

    111 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <tit ...