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. Docker在Ubuntu16.04上安装

    转自:http://blog.51cto.com/collen7788/2047800 1.添加Docker源 sudo apt-get update 2.增加CA证书 sudo apt-get in ...

  2. iOS 声明属性关键字讲解

    atomic: 原子操作(原子性是指事务的一个完整操作,操作成功就提交,反之就回滚. 原子操作就是指具有原子性的操作)在objective-c 属性设置里面 默认的就是atomic ,意思就是 set ...

  3. Java 基础入门随笔(11) JavaSE版——继承、覆盖、抽象类

    1.面向对象的特征二:继承 定义: 指一个对象直接使用另一对象的属性和方法. 继承好处: 1.提供代码的复用性. 2.让类与类直接产生了关系,给第三个特征多态提供了前提. java中支持单继承.不直接 ...

  4. ubuntu部署java环境

    一.安装java sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracl ...

  5. HDU_1022_Train Problem I

    Train Problem I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  6. mysqlworkbench 执行update语句报错:You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column

    You are using safe update mode and you tried to update a table without a WHERE that uses a KEY colum ...

  7. vue组件---动态组件之多标签页面

    首先看下效果图 代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> & ...

  8. @ExceptionHandler和@ControllerAdvice统一处理异常

    //@ExceptionHandler和@ControllerAdvice统一处理异常//统一处理异常的controller需要放在和普通controller同级的包下,或者在ComponentSca ...

  9. 【Hadoop】三、HDFS命令行接口

      通过前面对HDFS基本概念.高可用性.数据读写流程的介绍,我们对HDFS已经有了大致的了解.这里我们还需要明确一点:Hadoop作为一个完整的分布式系统,它有一个抽象的文件系统的概念,而我们介绍的 ...

  10. (C/C++学习)16.函数指针

    说明:函数指针,顾名思义就是指向函数的指针.C/C++中函数名的本质其实就是一段代码段空间的首地址. 1.定义 如下的 pf 就是一个函数指针,指向所有返回类型为 int,并带有两个 const in ...