Spring MVC Application - How do I set a session scoped bean value - spring

In my application I need to gather information on one screen and then display it on the next.
I have selected to store this information in a bean with a scope set as session ( it will be used in several other screens after the initial data gathering screen)
The Manager is configured as follows:
<bean name="/springapp.htm" class="foo.bar.controller.springcontroller">
<property name="sessionBeanManager" ref="sessionBeanManager" />
</bean>
The bean is configured as follows :
<bean id="sessionBean" class="foo.bar.sessionBean" scope="session">
<aop:scoped-proxy/>
<property name="beanValue" value="defaultValue" />
</bean>
<bean id="sessionBeanManager" class="foo.bar.sessionBeanManagerImpl">
<property name="sessionBean" ref="sessionBean"/>
</bean>
And I am outputting on the jsp page with
<c:out value="${sessionBean.beanValue}"></c:out>
but whenever I load the page the value is empty?
It seems to me that the bean is loading OK but is not populated with the value, which leads me to think that either the session bean is not being populated or the bean is not being created as a session bean?

You can reference spring session beans with the following syntax in your EL in the jsp.
${sessionScope['scopedTarget.messageUtil'].flashMessages}
That calls getFlashMessages() on this bean
<bean id="messageUtil" class="mypackage.MessageUtilImpl" scope="session">
<aop:scoped-proxy proxy-target-class="false"/>
<property name="messageSource" ref="messageSource" />
</bean>

Spring beans are not visible in the views (JSPs in your case) unless you add them first to the model.
You have to add your sessionBean to the model in the controller to make it available to the view.
model.addAttribute("sessionBean", sessionBean);

Related

Spring request scope bean not binding to same request in different DI instances

I have a class A that I want to be a request scope bean in two other classes.
In one class I write some data to the instance and in another class I attempt to read the data but it doesn't reference the same instance where the data is written even though it is in the same request.
My config is like this:
<bean name="blah" class="class.that.WritesToA">
<property name="A" ref="A"/>
</bean>
<bean name="whatever" class="class.that.ReadsFromA">
<property name="A" ref="A"/>
</bean>
<bean id="A" class="com.audit.Service" scope="request">
<aop:scoped-proxy />
</bean>
Am I missing something?

Setting property reference for a bean programmatically

Right now i am using the following bean entry
<bean id="Service" >
<property name="target">
<bean class="someClass" lazy-init="false">
<property name="SessionFactory1"><ref bean="SessionFactory1"/></property>
<property name="SessionFactory2"><ref bean="SessionFactory2"/></property>
<property name="SessionFactory3"><ref bean="SessionFactory3"/></property>
</bean>
</property>
</bean>
Now the requirement is to first check which all session factories have an active datasource and include those only in the above bean definition. So that the application does not break if we try to initialize a session factory with inactive datasource.
sessionfactory initialization will be take care by using seperate config xml for session factories and loading only the ones with active datasources.
Please help on how can this be achieved.
You can use Spring InitializingBean interface, which makes you implement an afterPropertiesSet() method. This method will be executed after Spring instantiates your class, and you could check if your session factories are available or not.
InitializingBean: Interface to be implemented by beans that need to react once all their properties have been set by a BeanFactory: for example, to perform custom initialization, or merely to check that all mandatory properties have been set.
link: Spring InitializingBean

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.

Spring El Expression

I'm switching from faces-config to Spring and wanted to know how you can pass a property from one bean to another:
e.g.
<bean id="myBean" class="Bean1">
</bean>
<bean id="myBean2" class="Bean2">
<constructor-arg ref="#{myBean1.value}"/>
</bean>
Upgraded to Spring 3.0 which has spring el support
First things first, the purpose of the D.I container is to fully initialize your system prior to execution; that is, all dependencies being set, the app is ready to run.
There are both #property and #value annotations in Spring for similar purposes, but since you want to use and specific bean property value for other bean the best solution would be:
<bean id="myBean" class="Bean1">
</bean>
<bean id="myBean2" class="Bean2">
<constructor-arg ref="myBean"/>
</bean>
If you argue that you just want to set the value at instantiation time, and not establish a dependency, then skip the D.I part and set the value directly.

Resources