spring injection works within user created instance? - java

I'm working with Spring, and I'm getting the injected instance as null when I create the instance with new operator, I can elobrate the scenario.
For example,
Let the class A and class B are injected into the class Main
class Main
{
#autowired
A a;
#autowired
B b;
//getter and setter
}
class MainExecute
{
public static void main()
{
// loading the spring config xml
Main main = new Main();
A a=main.getA();
// whether a will get the instance ( I'm getting a as null)
}
what could be the reason for this scenario
Please guide me on the same
Thanks in advance.

No, it won't get dependency injection.
Only objects created/managed by the spring container will get the facilities offered by spring dependency injection.

To elaborate on the above answers:
You need to let the spring IOC container create your objects for you, you don't explicitly create them. You can do this by creating a spring config xml file, here is a quick example:
META-INF\spring\my-spring-config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="myMainClass" class="org.foo.Main" />
<bean id="myA" class="org.foo.A" />
<bean id="myB" class="org.foo.B" />
</beans>
org.foo.MainExecute:
class MainExecute
{
public static void main()
{
ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext("META-INF\\spring\\my-spring-config.xml");
Main main = (Main) appContext.getBean("myMainClass");
}
}
In this example the Spring IOC container will instantiate an "A" bean and a "B" bean. It will then autowire these into the "Main" bean.

Related

Spring: Is it possible to specify the default-lazy-init value when you're new-ing the application context?

I'm creating my Spring application context from an XML file like so:
this.applicationContext = new ClassPathXmlApplicationContext("classpath*:/spring-configuration/application-context.xml");
I know there's an option to specify the default-lazy-init in the XML file itself, like in this example:
<?xml version="1.0" encoding="UTF-8"?>
<beans
...
default-lazy-init="true">
...
</beans>
However, I want to decide whether or not to use default lazy init based on the context when this code is invoked... is there a way to do this? I basically want an extra configuration boolean I could pass to the ClassPathXmlApplicationContext constructor which would tell it whether to use default lazy init or not.
Read Spring Docs On Lazy-initialized Beans :
ApplicationContext eagerly create singleton bean during startup. This
type of instantation is good because errors in the configuration are
found at the beginning. But if you
don't want the pre-instantiation of a singleton bean at the beginning, then you can mark that
bean definition as being lazy-initialized.
Note : A lazy-initialized singleton bean is instantiated only when it is requested.
You can achieve lazy initialization of bean in two ways :
-> At <bean> level :
<bean id="demoBean" class="com.example.DemoBean" lazy-init="true"/>
-> At container level (where no beans will be pre-instantiated) :
<beans default-lazy-init="true">
...
</beans>
So, select the desired once according to your need.
Example to understand :
DemoBean.java :
package com.example;
public class DemoBean {
public DemoBean(){
System.out.println("DemoBean is initialized on the request.");
}
}
Main.java :
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("application-context.xml");
context.getBean("demoBean");
}
}
Output:
DemoBean is initialized on the request.

Instantiating a Java Spring Bean with parameters in XML

I have two packages A and B with a class X within package B. I need to use an instance of a X in A.
Catch here is package B contains Java Bean spring configuration while A uses XML.
Here is how package B's AppConfig looks like.
#Configuration
public class PackageBJavaBeans {
#Bean
public X getX(final String paramOne, final String paramTwo) {
String value = doSomeProcessingWithParameters(paramOne, paramTwo);
return new X(value);
}
private String getXValue(final String paramOne, final String paramTwo){
final String value = //do-some-calculation
return value;
}
}
I need to create a bean of class X in package "A" with XML. How do I pass parameters via XML from package A?
Thanks.
I think this is what you're asking for.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for X bean -->
<bean id="X" class="A.X">
<constructor-arg value="The value this bean holds"/>
</bean>
</beans>
I'm a little confused on what exactly you want. Do you still want us to use the provided function that would concatenate the two strings together before creating X? That is possible using a factory method. Let me know if you want an example of a factory method bean.

Spring configuration for dependency injection

I'm learning spring dependency injection with Struts2, beased on a web project. In my example, I created a zoo having animals. Animal will talk if injection is succeed. E.g. in the console, we will see dog's talk :
Wowowo ฅ^•ﻌ•^ฅ
However, if injection failed, then we'll see :
zooService bean has not been injected.
Here's the architecture of my application :
com.zoo.controller.ZooController is the controller for receiving web actions.
com.zoo.service.ZooService is the interface for animal's talk
com.zoo.service.ZooServiceForDog is the implementation for dog's talk
Problem
Up to the step, everything is OK. And the dependency injection is handled by Spring using an XML file called applicationContext.xml. However, I've 2 types of configuration for this file, the first one Config 1 works but the second Config 2 doesn't.
Injection succeed using config 1.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="zooService" class="com.zoo.service.ZooServiceForDog" />
</beans>
Injection failed using config 2.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="zooController" class="com.zoo.controller.ZooController">
<property name="zooService" ref="zooServiceBean" />
</bean>
<bean id="zooServiceBean" class="com.zoo.service.ZooServiceForDog" />
</beans>
Can somebody explain why the Config 2 cannot work ?
Here're other codes that might be helpful to the issue :
Class com.zoo.controller.ZooController:
package com.zoo.controller;
import com.zoo.service.ZooService;
import com.opensymphony.xwork2.ActionSupport;
public class ZooController extends ActionSupport {
private static final long serialVersionUID = 1L;
private ZooService zooService;
public String live () {
if (zooService != null) {
zooService.talk();
} else {
System.out.println("zooService bean has not been injected.");
}
return SUCCESS;
}
public ZooService getZooService() {
return zooService;
}
public void setZooService(ZooService zooService) {
this.zooService = zooService;
}
}
It cannot work because the scope of the zooController is singleton. You should make the scope prototype.
<bean id="zooController" class="com.zoo.controller.ZooController" scope="prototype" >
<property name="zooService" ref="zooServiceBean" />
</bean>
The dependency management is defined by the container:
If your actions managed by Struts container, then Struts is creating them in the default scope. If your actions is managed by Spring container then you need to define the scope of the action beans, because Spring by default uses singleton scope and if you don't want to share your action beans between user's requests you should define the corresponding scope. You can use prototype scope, which means a new instance is returned by the Spring each time Struts is being built an action instance.
The Struts integrates to Spring via plugin. Make sure it has
<constant name="struts.objectFactory" value="spring" />
then you can delegate actions to Spring
References:
Struts2 and Spring
Spring plugin
EDIT:
In your first config you declared a bean zooService that will be injected by Struts using spring object factory.
In your second config you declared two beans zooController and zooServiceBean, but you changed the name of the second bean. Then you tried to build the action bean using spring object factory like in the first case. And because there's no bean with name zooService the autowiring has been failed. Because by default Struts is configured to autowire beans from the application context by name.
Then you changed struts.xml and used a bean reference in the action class attribute. It means that Struts will use app context to get a bean from Spring. And because it has an explicit dependency on the second bean, it would be wired before the bean is returned.

#Value annotation doesn't return a value

I have a class FichierCommunRetriever that use the #Value annotation of Spring. But I am struggling to make it work.
So in my application.properties I have :
application.donneeCommuneDossier=C\:\\test
application.destinationDonneeCommuneDossier=C\:\\dev\\repertoireDonneeCommune\\Cobol
My class FichierCommunRetriever is using those entries with the following code :
public class FichierCommunRetriever implements Runnable {
#Value("${application.donneeCommuneDossier}")
private String fichierCommunDossierPath;
#Value("${application.destinationDonneeCommuneDossier}")
private String destinationFichierCommunDossierPath;
}
We are loading application.properties with the following code in the class ApplicationConfig:
#ImportResource("classpath:/com/folder/folder/folder/folder/folder/applicationContext.xml")
In ApplicationConfig, I am defining a bean that use FichierCommunRetriever in a new thread like that :
Thread threadToExecuteTask = new Thread(new FichierCommunRetriever());
threadToExecuteTask.start();
I suppose that my problem is, since FichierCommunRetriever is running in a separate thread, the class can't reach the applicationContext and is not able to give a value.
I'm wondering if the annotation would work or I must change the way I'm getting those values?
In your applicationConfig you should define your bean this way:
#Configuration
public class AppConfig {
#Bean
public FichierCommunRetriever fichierCommunRetriever() {
return new FichierCommunRetriever();
}
}
Then, after Spring loads, you can access you bean via the application context
FichierCommunRetriever f = applicationContext.getBean(FichierCommunRetriever.class);
Thread threadToExecuteTask = new Thread(f);
threadToExecuteTask.start();
Now you are sure that your bean lives inside Spring context and that it is initialized.
In addition, in your Spring XML, you have to load the properties (this example uses the context namespace):
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
...
<context:property-placeholder location="classpath:application.properties" />
...
</beans>
You create an instance of FichierCommunRetriever using new, instead of asking Spring to return a bean instance. So Spring isn't controlling the creation and injection of this instance.
You should have the following method in your config class, and call it to get the bean instance:
#Bean
public FichierCommunRetriever fichierCommunRetriever() {
return new FichierCommunRetriever();
}
...
Thread threadToExecuteTask = new Thread(fichierCommunRetriever());

Loading an application context by annotating a class

I am new at Spring and am wondering if one can load an application just by annotating the class whose variables must be injected (instead of using ApplicationContext ctx = new ApplicationContext("myAppContext")).
Let me give the following example:
I have this class TestSpring.java in which a string should be autowired
package mytest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
//Is it possible to put an annotation here that loads the application context "TestSpringContext.xm"??
public class TestSpring {
#Autowired
#Qualifier("myStringBean")
private String myString;
/**
* Should show the value of the injected string
*/
public void showString() {
System.out.println(myString);
}
}
The spring bean configuration file (TestSpringContext.xml) looks like this
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
>
<context:annotation-config />
<bean id="myStringBean" class="java.lang.String">
<constructor-arg value="I am an injected String."/>
</bean>
</beans>
Now I would like to display the value of the autowired string myString (declared in TestSpring.java) using following code in RunTestSpring.java:
package mytest;
public class RunTestSpring {
public static void main(String[] args) {
TestSpring testInstance = new TestSpring();
testInstance.showString();
}
}
Now my question, is it possible to run "RunTestSpring.java" successfully while loading the application context by just annotating RunTestSpring.java. If yes, with which annotation?
#Configurable is probably what you are looking for, it will ensure that objects which are not instantiated by Spring can have their dependencies autowired by Spring. However the catch is that it requires AspectJ compile time/load time weaving for it to work(not Spring AOP).
Here is one reference:
http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/aop.html#aop-atconfigurable
I would suggest writing a JUnit class that would use spring injection for environment initialization. Something like this -
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations="/spring/spring-wireup.xml", inheritLocations = true)
public class MyTestCase extends TestCase {
// your test methods ...
}

Categories