What to put into applicationContext and faces-config? - spring

I created a JSF Application and I'm not sure what I should put into the faces-config?
One of my ManagedBeans looks like the following:
#ManagedBean(name = "ProfileBean")
#ViewScoped
public class ProfileBean implements Serializable
my applicationcontext
<context:annotation-config />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />
<bean id="QuestionDao" class="code.elephant.dao.QuestionDao"></bean>
<bean id="QuestionService" class="code.elephant.service.QuestionService">
<constructor-arg ref="QuestionDao"/>
</bean>
<bean id="QuestionBean" class="controller.QuestionBean">
<constructor-arg ref="QuestionService"/>
</bean>
<bean id="UserDao" class="code.elephant.dao.UserDao"></bean>
<bean id="UserService" class="code.elephant.service.UserService">
<constructor-arg ref="UserDao"/>
</bean>
<bean id="LoginBean" class="controller.LoginBean">
<constructor-arg ref="UserService"/>
</bean>
<bean id="ProfileBean" class="controller.ProfileBean">
<constructor-arg ref="UserService" />
<property name="_LoginBean" ref="LoginBean"></property>
</bean>
my faces-config
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
I saw some examples which defines <managed-beans> in the java-faces.config but I already used the #ManagedBean annotation in the java class. Is this rly necessary? Is my setup up with jsf spring correct? Should I also define the managed beans in the faces-config?

The purpose of the SpringBeanFacesELResolver that you have configured in faces-config is to make it so that you can use Spring beans instead of the old-style JSF managed beans or CDI dependency injection.
Remove the #ManagedBean annotation from your ProfileBean class. You don't need it since you are using Spring instead of JSF's old managed beans mechanism.
The #ManagedBean annotation is a remnant from old versions of JSF; don't use it if you are using a newer version of JSF. Current versions of JSF use CDI (the standard Java EE API for dependency injection), but you are using Spring instead, so you should configure your beans the Spring way (which you are already doing since you've defined ProfileBean in your Spring XML config).

Related

Working of Resource Injection #Resource and Spring Framework?

I want to understand how the annotation works. I have this piece of code which I have used in a simple Spring project.
#Resource(name="dataSource")
private DataSource dataSouce;
The dataSource I have defined in an XML config file:
<!-- The Apache DBCP implementation of DataSource -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="org.h2.Driver"/>
<property name="url" value="jdbc:h2:tcp://localhost/~/test"/>
<property name="username" value="sa"/>
<property name="password" value=""/>
</bean>
Other snippet from spring xml config file
<context:annotation-config /> <!-- This enables the annotation's actions, else annotations don't do their work. -->
<context:component-scan base-package="com.example.dao.jdbc.impl"/> <!-- This is for component scan -->
<bean id="jdbcOperImpl" class="com.example.dao.jdbc.impl.JdbcOperImpl"/>
As I understand the Resource annotations comes from javax.annotation.Resource. I looked its source code, and I notices the annotation is defined by JDK SE, and it is just a simple definition of an annotation. How does this do the injection? Does Spring Framework use this annotation and does Injection? How is #Resource annotation and Spring framework related?
Actually Spring supports two types of annotations which are Spring based annotations and Java based annotations. For dependencies you can use #Autowired that completely spring annotations.
#Resource and #Inject are the standard java based annotations. For more clarity see this link http://blogs.sourceallies.com/2011/08/spring-injection-with-resource-and-autowired/

spring batch: inject main application context #Component using AutomaticJobRegistrar

I'm trying to inject some #Service / #Repository bean defined in main application context into some jobs loaded by AutomaticJobRegistrar.
<bean class="org.springframework.batch.core.configuration.support.AutomaticJobRegistrar">
<property name="applicationContextFactories">
<bean class="org.springframework.batch.core.configuration.support.ClasspathXmlApplicationContextsFactoryBean">
<property name="resources" value="classpath*:/META-INF/jobs/*Job.xml" />
</bean>
</property>
<property name="jobLoader">
<bean class="org.springframework.batch.core.configuration.support.DefaultJobLoader">
<property name="jobRegistry" ref="jobRegistry" />
</bean>
</property>
</bean>
Using #Autowired inside my ItemWriter implementation class do not inject my services beans.
I have to enable component scanning inside each *Job.xml or declare each bean in order to make injection works, but injected classes are not the same instance of that one used by main application context.
How can I get the same instance bean declared in main application context?
Thank you for any advice
Did you activate context:annotation-config for each job?
If a new applicationcontext is created for every Job you need to activate this or no annotationprocessing (including #Autowired) will happen.
'context:component-scan' also activates 'context:annotation-config' so this might be the reason #Autowired works if you activate it.

When does spring use AOP without Aspectj.jar?

I can use annotation #Transactional at the top of my service PersonServiceBean and config transactionManager like below. After that, my service can execute db operations under transaction control through AOP.
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
.........
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
<bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
<property name="dataSource" ref="dataSource"/>
</bean>
But i didn't include aspectj.jar into my project. How does it work? I heard spring implements AOP through aspectj.
It doesn't use AspectJ by default, but uses proxies (either JDK interface proxies or CGLIB proxies). So, when you inject a transactional bean into another bean, what you get injected is in fact a proxy to your actual bean instance, which intercepts the method calls and starts/commits/rollbacks transactions.
More in the documentation, of course.

Spring, XML beans call Annotation beans when app start

I have one Annotation bean with some methods. It works fine.
public #Controller("adminController") class AdminController {
...
private #Autowired AdminDAO adminDAO;
public void resetTemporalList() {
System.out.println("HE SIDO EJECUTADO.");
this.adminDAO.resetTemporalRegisters();
}
...
}
Now, I am integrating one quartz task. But I am load it with XML definition beans that call previus annotation bean.
<bean id="resetTemporalRegisters" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminController" />
<property name="targetMethod" value="resetTemporalList" />
<property name="concurrent" value="false" />
</bean>
Whan I start my app appear next error.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'adminController' is defined
I believe the problem is that Spring load XML beans first, after Annotation beans, then in this moment "adminController" bean not exits...
How Can I fix it?
SOLVED IT!!
Problem was in I put xml bean definitions in applicationContext.xml.
No, XML and annotations integrate fine, but do you actually have the component scanning code in your XML?
<context:component-scan base-package="com.yourcompany.yourapp"/>
See: 4.10 Classpath scanning and managed components
A little bit of guessing: your controller is defined in child application context created by Spring MVC while you resetTemporalRegisters job in main application context (parent). Child context can access beans from parent context but not the other way around.
This raises important question: why is your business logic trying to call a method of a controller? These methods should be called only be the MVC framework. Can't you just call
this.adminDAO.resetTemporalRegisters();
directly from your job?
<bean id="resetTemporalRegisters" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="adminDAO" />
<property name="targetMethod" value="resetTemporalRegisters" />
<property name="concurrent" value="false" />
</bean>
adminDAO is probably defined in parent context, so you can access it easily.

Configuring Datasource in Spring 3.0

Hello guys I have configured a connection pool and JNDI resource in glassfish 2.1. I can get the Datasource via lookup method in my projects and everything works good. However I decided to try Spring framework and to use my existing connection pool.
In the Spring context file I have the following:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
<property name="simpleJdbcTemplate" ref="jdbcTemplate"/>
</bean>
When I deploy the project I get:
java.lang.IllegalArgumentException: 'dataSource' or 'jdbcTemplate' is required]
Is there anything else I have to configure in that file or in any other file in order to get the Datasource?
Presumably, com.mycompany.mavenproject3.Dao extends JdbcDaoSupport, but you're setting a property named simpleJdbcTemplate on it, leading me to believe that you've defined your own property to hold the template since that doesn't exist on Spring's implementation. It's therefore complaining at you because you're required to set either the dataSource property or the jdbcTemplate property of the JdbcDaoSupport object before using it, exactly like it's telling you. Change <property name="simpleJdbcTemplate"... to <property name="jdbcTemplate"....
If your DAO doesn't extend JdbcDaoSupport, then find what does and remove it or set its properties appropriately.
You can also call your datasource directly in your dao bean, don't need to do an another bean for jdbcTemplate. So your context file become something like this:
<jee:jndi-lookup id="dataSource" jndi-name="jdbc/name" />
<bean id="dao" class="com.mycompany.mavenproject3.Dao">
<property name="dataSource" ref="dataSource"/>
</bean>
After you just have to extends JdbcDaoSupport spring class (in which contain the getter and setter of datasource) on your Dao class.

Resources