I have a mediaOutput tag which, in its createContent attribute, requires the backing bean to be in a certain state. A list of values, which is filled in an init method, must be available.
I therefore added a keepAlive tag for the whole backing bean. I now indeed see the backingBean in stead of some (richfaces) proxy bean, but the filled list is null again.
How to make this possible?
I checked that the init method was called and that the list is filled in in the init method.
<a4j:keepAlive beanName="myBean" />
<a4j:mediaOutput createContent="#{myBean.writeChart}" ... />
The backing bean
public class MyBean implements Serializable {
public List list;
public void init(ActionEvent event) {
// call some resource to fill the list
list = service.getItems();
}
public void writeChart(final OutputStream out, final Object data) throws IOException {
// list is null
}
// getters & setters
}
Declare your bean to be in session scope.
If you have other request-only information in the bean, then just create a new request-scoped bean and move all the other stuff there. It's perfectly legible.
This is not a problem. You don't have to keep the Mediabean alive, and you can't. The bean which is given in the createContent parameter will be created by the MediaOutput component. The "bean" prefix is the disturbing one - this is only a simple java class which contains the paint(...) method. You have to get the keepalived bean (for example a backing bean) in this simple "bean" as a ManagedProperty, and it can contain the keepalived information too.
Example:
abc.xhtml and ABC.java with #ManagedBean(name = "ABCBean") and #RequestScoped annotation.
You use ABCBean as a Backing Bean with the abc.xhtml, but NOT in the mediaOutput.createContent parameter! But you can create MediaBean.java with #ManagedBean(name="MediaBean") annotation, and it has a #ManagedProperty which gets the ABCBean instance in the MediaBean. And the ABCBean instance is keepalived...
Related
I followed the following example of dependency injection: http://www.tutorialspoint.com/spring/spring_autowired_annotation.htm
For example the TextEditor class (from the above link):
public class TextEditor {
private SpellChecker spellChecker;
#Autowired
public void setSpellChecker( SpellChecker spellChecker ){
this.spellChecker = spellChecker;
}
public SpellChecker getSpellChecker( ) {
return spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
How can these dependencies/classes be instantiated, while they don't have any constructor?
Is Java simply making an object of that type, that is empty? Like an empty parameter constructor without any code?
Thanks for making this more clear!
Unless specified otherwise, every Java class has the default constructor. So here, you have a default public TextEditor() constructor, even though you haven't coded for it. (You could code it if you needed to change its visibility from public, declare a thrown exception, etc.)
So yes, Spring calls this default constructor - then calls the setSpellChecker method (as annotated, and through reflection) to populate it.
If no constructor is defined, a class can be instantiated via the no-argument default constructor.
So, the framework calls that constructor (supposedly using reflection) and then uses the set method to set the one field of the freshly created class.
The example above is using Spring annotations and Spring context file and those are the main and most important parts of the project, considering the DI.
So in the context file you have following line:
<!-- Definition for spellChecker bean -->
<bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
this defines a class with reference spellChecker that mapps to a class com.tutorialspoint.SpellChecker and once the compiler find such property in a method that is marked as #Autowired on instantiation of the object it injects/sets the relevant version of the required dependency.
In cases where a property doesn't match a reference tag in the applicationContext.xml file Spring is trying to map the type e.g. property with name mySpecialSpellChecker which has type of com.tutorialspoint.SpellChecker still will be mapped to bean with id="spellChecker" if there are more than one of same type Spring won't instantiate your object and you might get compile time error as Spring can't know which version of the two (or more) is the correct one so this requires developer input.
This is the order of execution:
instantiate textEditor, this has default constructor that is not visible in the code public TextEditor ()
the new instance is set in a pool of available objects with reference textEditor
instantiate spellChecker and add to the pool of available object with relevant reference/label
all #Autowired properties/methods are set/called with relevant objects in this case Spring calls: setSpellChecker(spellChecker)
assume I have that class
public Student{
private String name;
private Address address;
public Student(String fName, Address address){
name = fname;
this.address = address;
}
I defined this class within Spring configuration as
<bean name="studentInstance" class="StackOverFlow.Student"/>
now i'd like to use getBean with parameter I will pass to constructor.
equal to Student s = new Student(name,address)
I know Spring supplies a methond getBean(class_name,parms....)
however I dont know how I should config Spring.xml configuration file.
I would like to avoid using Setter and getter in order to fill a new bean.
I found lots of example of how to define </constructor-arg> within the xml but each time it was with default values. here I let the user to enter different values for each object.
I'd like to use
ApplicationContext context = new ClassPathXmlApplicationContext(Spring.xml file path);
Student s= (Student)context.getBean("studentInstance",name,address);
I need help with the configuration file only
Thanks in Advance!!
I already checked those links :
Link1 Link2 Link3 Link4
~~~~~Edit ~~~~~~~
Solved! constructor-injection is not needed here
I just added prototype scope to my bean as shown below.
<bean name="carInstance" class="MainApp.bl.GasStation.Car" scope="prototype"/>
Firstly, such bean must obviously be declared as prototype.
The Prototype scopes a single bean definition to have any number of object instances. If scope is set to prototype, the Spring IoC container creates new bean instance of the object every time a request for that specific bean is made
<
Object getBean(String name, Object... args)throws BeansException
Return an instance, which may be shared or independent, of the specified bean.
Allows for specifying explicit constructor arguments / factory method arguments, overriding the specified default arguments (if any) in the bean definition.
Refer to following question for configuration:
Spring <constructor-arg> element must specify a ref or value
Note, you will have to wrap primitives into their Wrapper objects to avoid having predefined values when object is created.
I have a web application that executes on tomcat 6.
I have a MysqlDb class that uses a BasicDataSource from a spring JDBC.
so far I've used the following bean configuration in web.xml:
<bean id="MysqlDb" class="com.xpogames.gamesisland.mysql.MysqlDb">
<property name="idDataSource" ref="idDataSource"/>
</bean>
and I had the following setter function:
public void setidDataSource(BasicDataSource ds) {
this._dataSource=(DataSource)ds;
this._simpleJdbcTemplate = new SimpleJdbcTemplate(_dataSource);
this._jdbcTemplate = new JdbcTemplate(_dataSource);
}
I want to convert my class to use static functions, so I created an empty private constructor so the class won't explicitly instantiated by callers.
besides that I changed the setidDataSource function to a static function, but when I try to do that I get the following error:
Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'idDataSource' of bean class [com.xpogames.gamesisland.mysql.MysqlDb]: Bean property 'idDataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
is there a way to resolve this issue in web.xml or do I need to manually
fetch the ServletContext
ServletContext servletContext = this.getServletContext();
this._context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
and fetch the bean from there and just remove the lines i printed here from web.xml ?
For one, you've declared the setter setidDataSource. It should be setIdDataSource. The first letter of a property must be a capital letter after the word set.
Also, a setter method must not be static but an instance method.
Spring-beans are by default singletons, you do not need to implement your class as a singleton as long as you use the bean from the context.
The simplest answer to your question is getting the bean from the context after setting the datasource in the context, but I thought you want to stay away from the context.
Static setters for class are of course possible (they would just set static property for all instances), but in IoC pattern (which is used in spring), bean is instance, and term "property of bean" always means "property of instance of class" - consider it as limitation of given IoC implementation.
I understand know that I had a bad implementation idea. I still need a constructor, so building a class with static functions and static init is a bad idea, and trying to execute a static setter from a bean is not logical and impossible.
Instead I changed the class to be a singleton class, so I will be able to use it anywhere in my application and it will be constructed only once.
thanks for all the information.
update
I still don't know if that's a good method, but at least it works.
in my red-web.xml (consider it as spring applicationContext.xml), I have the following:
<bean id="MysqlDb" class="com.xpogames.gamesisland.mysql.MysqlDb" init-method="getInstance">
<property name="idDataSource" ref="idDataSource"/>
</bean>
Here it creates a MysqlDb bean and configure it to use the getInstance() init method if MysqlDb Class. i made sure to have a setidDataSource() function in mysqlDb class for the datasource to be properly set.
<bean id="web.handler" class="com.xpogames.gamesisland.Application">
<property name="MysqlDb" ref="MysqlDb"/>
</bean>
Here, I create the main bean of my application and I made sure to have the function setMysqlDb for the MysqlDb class to be set from the bean configuration.
So far mysqlDb acts as a singelton class because it's constructor is protected and it creates the instance only once:
public static MysqlDb getInstance() {
if (instance == null) {
instance = new MysqlDb();
}
return instance;
}
The problem that I encountered was that in other parts of my application whenever I used getInstance(), the MysqlDb class would come up and all the variables that where set with setidDataSource where null.
so resolve that issue I created another function called setInstance in mysqlDb:
public static void setInstance(MysqlDb db) {
instance=db;
}
this is my main setMysqlDb function in my main application:
public void setMysqlDb(MysqlDb db) {
this._mysqlDb=db;
/* it seems that without forcing a setInstance on the class, whenever other classes
* would try to getInstance(), variables that are supposed to be configured by the bean
* would be empty. this resolves that issue
*/
MysqlDb.setInstance(db);
}
so this configuration works. it's obviously not the recommended or best solution! but it seems that I need to read and learn further before I come up with a better solution.
I have the archetype org.jboss.weld.archetypes:jboss-javaee6-webapp:1.0.1.CR2 and I try to understand the class MemberListProducer:
#RequestScoped
public class MemberListProducer
{
#Inject #MemberRepository private EntityManager em;
private List<Member> members;
#Produces #Named public List<Member> getMembers() {return members;}
public void onMemberListChanged(#Observes(notifyObserver = Reception.IF_EXISTS)
final Member member){
retrieveAllMembersOrderedByName();
}
#PostConstruct
public void retrieveAllMembersOrderedByName()
{
//Criteria Query to fetch all members
members = em.createQuery(criteria).getResultList();
}
}
The observer is invoked from another class with memberEventSrc.fire(newMember);, this seems clear: Once fired, the MemberListProducer updates the list of members.
But I don't understand why this is done in a #RequestScoped Bean. In my understanding the method retrieveAllMembersOrderedByName is anyway called by each request. Should this #Observes not be better placed in a #ViewScoped or #SessionScoped Bean? Does it have an effect in this case at all?
The use of #Observes there is more of an example than a real, practical use case. Consider the possibility of members changing before you render your response.
I don't think the website would work correctly if you removed it. Think about it like this:
When the request starts, the list of members is created and it contains all the members up to the moment of creation of this request scoped bean. Later, you persist a new member, so this list needs to be updated to render the response.
You think correctly when you say that the list is built for each request, however this happens at the beginning. After you add a member, you need to refresh it, don't you?
If this method weren't there, the response would be outdated (you'd render the list you had before you persisted the new member), and you would need one extra post or get to fetch the new list of members.
#Observes decouples listeners and event sources much like the observer pattern. So if the #Observes isn't there, you would need to explicitly add the new member to the list so that the response is correct.
I hope I understood your question correctly.
It's request scoped because it stores a list of members per request. If you need this list to be stored per-session, then change it.
But it looks wrong - you are discarding the member arguments of the observer method.
I'm having some problems with defining a binding bean for my rich:dataTable.
The contents of the datatable are already defined in the XHTML, I just want to use the binding to read the rows back later so that the table contents can be exported into a PDF.
In my XHTML file, I put
<rich:dataTable ... binding="#{backingBean.tableBinding}">
Then, in my backing bean, I declare a member:
import org.richfaces.component.html.HtmlDataTable;
class BackingBean {
private HtmlDataTable tableBinding ;
public BackingBean() {
tableBinding = new HtmlDataTable() ;
}
// ... (incl. getters and setters for binding)
} ;
However, this results in:
java.lang.NullPointerException
at javax.faces.component.UIComponentBase.encodeBegin(UIComponentBase.java:809)
when I try to load the page. Is there something I need to do with the HtmlDataTable variable besides simply constructing it? Is it even possible to use the binding attribute in the way I intend here?
Don't create an instance of HtmlDataTable yourself in the constructor. RichFaces will create an instance and inject it to your BackingBean. All you need is an public getter and setter.