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.
Related
I am new to spring and saw this approach for resolving method parameter from properties file...
public void setUserDetails(#Value("${user.first.name}")String userName,
#Value("${user.address}") String address) {
this.userName = userName;
this.address = address;
}
but whenever i call this function the values are replaced by what i pass to function. i want to know how to call the function so that the values are fetched from properties file.
I am calling the function like...
setUserDetails("abc","xyz")
setUserDetails("abc",null)
setUserDetails(null,"xyz")
setUserDetails(null,null)
setUserDetails(null) // invalid
setUserDetails() // invalid
Everytime i get what i pass not from the data from properties. Please help to find me some solution or some reference link which may be helpful...
As mentioned on https://stackoverflow.com/a/21769581/679240, that annotation won't interfere on a manual invocation of the method; instead, it will automatically invoke the method right after instantiating the bean.
As the manual states:
Fields are injected right after construction of a bean, before any config methods are invoked. [...] Bean property setter methods [as in this case] are effectively just a special case of such a general config method.
Spring annotation works on bean creation, not during methods invocation. Suppose your class is called UserDetails:
#Component
public class UserDetails{
public #Value("${user.first.name}") String userName;
public #Value("${user.address}") String address;
}
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)
In my Java class I have a field of type, say, java.util.Properties or java.util.Map.
I'd like Spring to inject the field with all properties which start with the given prefix. Ideally by specifying wildcard in the property annotation, smth like #Value(${prefix.*}) that doesn't seem to work unfortunately.
How can I achieve that?
AFAIK there isn't a direct way to achieve what you are looking for.
You can however use Spring EL in combination with custom implementation of org.springframework.core.io.support.PropertiesLoaderSupport.
First extend the PropertiesLoaderSupport (or any of it's subclass). Introduce a method (say filterProps), which will return java.util.Properties object with filtered properties, such as below
public class CustomPropertyLoader extends PropertiesLoaderSupport {
public Properties filterProperties(String prefix){
Properties temp = new Properties();
for(Properties props : this.localProperties){
/*
* Iterate over props and filter them as
* per prefix (or any custom logic) to temp
*/
}
return temp;
}
}
TIP - You can use a regex for more generic scenarios instead of String as method argument.
Second define a corresponding bean of above class
<bean id="customPropertyLoader" class="x.y.z.CustomPropertyLoader">
<property name="locations" value="classpath*:/**/some*.properties"/>
</bean>
Last annotate the field of type java.util.Properties in your class as
#Value(#{customPropertyLoader.filterProperties('prefix.*')})
P.S.: Please excuse any compilation / syntax error as I haven't compiled and check the setup; but it should work. In case not, let know in comments.
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 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...