跳至主要内容

博文

Annotations in JBoss Deployment

在我们书写EJB代码时,为了简化配置,我们使用了注解而不是xml配置文件。那么,JBoss(或者说Wildfly)是如何利用这些注解,在合适的时候调用了我们的代码呢?换句话说,jboss从启动到输出像这样的jndi设置信息的部署过程究竟发生了什么?今天我们就通过分析其源代码简单探讨一下这个问题。 从annotations讲起 在开发过程中, 我们也时常会在应用代码中会看到这些看上去比较奇怪的@开头的东西,例如@Override, @Deprecated。他们的作用到底是什么?又是如何实现的呢?我们稍稍介绍以一跟我们今天内容相关的背景。 注解,根据其寿命长短,可以分为三类: Source:只存在于源代码中,在编译后便被丢弃。所以说他的作用范围便是编译时。 Class:在class文件中,但不在VM中。貌似没有什么卵用 Runtime:一直存活到VM中,即可以通过反射获得注释信息(用大白话说就是,当你程序跑起来之后,还可以获取注解的内容)。 所以,联想到我们在使用EJB的过程:先写好源代码,再编译,再打包,再交给jboss部署,jboss再调用我们的代码。我们可以明白,诸如@Stateless, @EJB这样的注解都是第三类注解。 JBoss部署 在上面讲的使用EJB的过程中,jboss的部署其实还是一个黑盒,所以当我们把我们的jar包部署到JBoss时究竟发生了什么? 让我们通过源代码(org/jboss/as/server/deployment/Phase.java)把部署过程理一理: /** * This phase creates the initial root structure. Depending on the * service for this phase will ensure that the * deployment unit's initial root structure is available and accessible. * Upon entry, this phase performs the following actions: * <ul> * <li>The primary deployment root is mounted...

Learn struts2 and JPA: by trial and error

This blog shares some errors we met, and some problem we encountered, to assist future reader. org.hibernate.LazyInitializationException Detailed error message is following: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: entity.Branch.plans, could not initialize proxy - no Session Here is a strange solution for lazy fetch and finally I give bidirectional relationship and this problem solved. @OneToOne with Shared Primary Key Code sample: public class User { @Id Integer id; @OneToOne (mappedBy = "user" ) @PrimaryKeyJoinColumn Account account; } public class Account { @Id Integer id; @MapsId @OneToOne @JoinColumn (name = "uid" ) User user; public Account (User user) { this .user = user; /* id = user.getUid(); can't assign it, why? error: null value was assigned to a property of primitive type setter uid ...

Intellij Java EE Errors

Develop a web application using Tomcat Create project Choose a Java Enterprise project and select the Web application Framework, select your application server( If you don’t have one in idea, just click new and choose the root folder of your server to create one). Click Finish and you are ready to write your servlet Add JDBC support using DataSource and JNDI( taking mysql as an example) add driver to server-root-folder/lib/ add context.xml to context-root/META-INF/ (create one if not exist) with the following content: < Context > < Resource name = "jdbc/YourJNDIRegisteredName" auth = "Container" type = "javax.sql.DataSource" maxTotal = "50" maxIdle = "30" maxWaitMillis = "10000" logAbandoned = "true" username = "root" password = "" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://localhost:3306/YourMysqlDBName...

Patterns Learned Fom `Iterator`

Pattern one: When it comes to iterators, we can easily come up with that its use in abstracting the container and simplify the iteration operation like following example shows: Iterator it = container . iterator () while ( it . hasNext ()) { a = it . next (); // do something } No matter which kind of container you are using (the container has to be iterable and as far as I know, the commonly-used container are all iterable.), you can re-use this piece of code without any change. And this may be extract as a method, like following: public void doIterate ( Iterator it ) { while ( it . hasNext ()) { a = it . next (); // do something } } You can even make it more general with another parameter: public void doIterate ( Iterator it , Function f ) { while ( it . hasNext ()) { a = it . next (); f ( a ); } } This kind of design pattern can isolate the module of handling elements in container, whic...

Singleton and Synchronization

Why synchronized in singleton I will describe what I encounter to convince you it is really needed to do so. I have the Class A in the Thread-use : Singleton s1 = Singleton.getInstance(); synchronized(s1){ while (!B.ready){ s1.wait(); } // read from singleton } Then Class B in Thread-readData : // notice the volatile static volatile boolean ready = false; Singleton s2 = Singleton.getInstance(); synchronized(s2){ // read data than write to singleton ready = true; s2.notify(); } Then Singleton : public static Singleton getInstance() { if(instance == null) { instance = new Singleton(); } return instance; } This getInstance() is not synchronized now. If the first thread enter the if clause first, then the second thread get the cpu also enter it ( if clause ) when the instance are still null , so they create two Singleton and return separately . As a result, the s1 and s2 point to the different objects which cause...

Learning Note: constant

一. Java Java use `final` to represent a constant: compile-time constant value initialized at run-time but not change ever since data can be: primitive -- the data can't be changed object reference -- the reference can't change but the object can change // can also be compile -time constants:   public static final int VALUE_THREE = 39 ; // can also be run-time constants: private final int i4 = rand . nextInt ( 20 ); static final int INT_5 = rand . nextInt ( 20 ); // a final reference private final String s1 = new String ( "s1" );   二. c++ c++ using the keyword `const` to address this: compile-time constant value initialized at run-time but not change ever since (see following -- cited from stackoverflow) As a static or function-local variable: const int x = calcConstant();     As a class member: struct ConstContainer { ConstContainer( int x) : x(x) {} ...