Is there any XML equivalent of #Configurable annotation?
For example for the bean:
<bean class="org.obliquid.sherd.domain.SalesDocument" scope ="prototype">
<property name="docType" ref="documentTypeProto"/>
</bean>
How can I tell that SalesDocument should be #Configurable?
No - the purpose of #Configurable to inject properties into objects that are not Spring beans. In your example SalesDocument is already a Spring bean, and docType will be injected.
There's no way to simply do this out of the box that I'm aware of.
One approach to achieve what you want is to look at using Spring AspectJ based AOP extensions. It'll be a lot of work, but if you know that your DAOs need limited configuration (probably the just the EntityManager?) it might be doable.
Look at the spring reference docs for details.
Related
I need to inject a object of a java class in spring controller through applicaionContext.xml. My controller will be ,
#Controller
public class SpringController{
private MyClass obj;
}
I know I can do it with #Autowired annotation.
Is this really good to create a object for a controller through applicaionContext.xml ? Also can I inject a object of a class in controller using the <property> tag inside a <bean> tag ?
Is this really possible ? or please forgive me if it is a stupid question.
I need to know the possible ways for how to inject a object of a class in Spring controller ?
You can of course use #Autowired annotation to autowire the relationships, which can reduce the need to define the properties and constructor arguments for the controller in your applicationContext.xml file. And also to add a dependency to a class, you don't need to modify the configuration files.
But it has some disadvantages too, like if you use #Autowired, there will not be any explicit documentation for the wiring details between Spring managed beans. And to know the relationships between the beans, you have to go through your managed beans. But, if you use configuration files to define the relationships, the relationship details can be found in one place.
You can inject an object of a class into your controller through your applicaionContext.xml as below:
Constructor based injection:
#Controller
public class SpringController{
private MyClass obj;
public SpringController(MyClass obj){
this.obj=obj;
}
}
<bean id="myClassImpl" class="x.y.z.MyClassImpl"></bean>
<bean id="springController" class="x.y.z.web.controllers.SpringController">
<constructor-arg ref="myClassImpl"></constructor-arg>
</bean>
Setter based injection:
#Controller
public class SpringController{
private MyClass obj;
public void setObj(MyClass obj){
this.obj=obj;
}
public MyClass getObj(){
return obj;
}
}
<bean id="myClassImpl" class="x.y.z.MyClassImpl"></bean>
<bean id="springController" class="x.y.z.web.controllers.SpringController">
<property name="obj" ref="myClassImpl"></property>
</bean>
If you want to inject an object in a controller and you particularly want to you use xml,then instead of component scanning of Controller you should create a bean of the controller class of singleton scope in the application context.
Your controller class need not be annotated with #Controller.
you then have to you extend some Controller also like AbstractCommandController, AbstractController, AbstractFormController, AbstractWizardFormController, BaseCommandController, CancellableFormController, MultiActionController SimpleFormController, UrlFilenameViewController
Now to inject a particular object you can use Either Constructor and Setter based injection.
or you can use Autowring by name or type to auto inject the object.
Make sure that you have also declared the bean of that object also in Application Context.
After a DispatcherServlet has received a request and has done its work to resolve locales, themes and suchlike, it then tries to resolve a Controller, using a HandlerMapping. When a Controller has been found to handle the request, the handleRequest method of the located Controller will be invoked; the located Controller is then responsible for handling the actual request and - if applicable - returning an appropriate ModelAndView.
Thats it.
Actually, injection with xml and annotation is same behind the scene. Xml is old fashion while annotations are newer.
Basically, there are 2 types of injection types.
byName
Autowiring by property name. Spring container looks at the properties
of the beans on which autowire attribute is set to byName in the XML
configuration file. It then tries to match and wire its properties
with the beans defined by the same names in the configuration file.
You can give explicit names to beans both with xml and annotation.
#Service("BeanName")
#Component("BeanName")
#Controller("BeanName")
<bean name="BeanName" class="someclass"></bean>
and inject beans by using #Qualifier annotation.
#Autowired
#Qualifier("BeanName")
and with xml
<bean id="MyBean2" class="MyBean2 class">
<property name="Property of MyBean2 which refers to injecting bean" ref="BeanName" />
</bean>
byType
Autowiring by property datatype. Spring container looks at the
properties of the beans on which autowire attribute is set to byType
in the XML configuration file. It then tries to match and wire a
property if its type matches with exactly one of the beans name in
configuration file. If more than one such beans exists, a fatal
exception is thrown.
Default auto wiring mode is byType, so spring will look for matching type in auto wiring. However, older versions of Spring has default behavior none on injection. If you want to inject byType using xml, you should tell spring contaioner explicitly.
For example MyBean2 has a reference to MyBean, by setting autowired attribute to byType it handles injection automatically.
<bean id="MyBean" class="MyBean class">
<property name="Property of MyBean2 which refers to injecting bean" ref="BeanName" />
</bean>
<bean id="MyBean2" class="MyBean2 class"
autowire="byType">
</bean>
It also depends on where the injection take place in your code. There are 2 types, setter getter injection and constructor injection.
Note : There is no difference in #Controller since they are already in spring context.
See also
Spring Beans Auto wiring
I ran into such problem. I was getting "Ambiguous mapping found". (I use xml configuration as well and i am injecting a bean into my controller)
Then looking at my console i realized that my controller was being instantiated twice.
In more detailed look i noticed that my annotation
#Controller(value = "aController")
(Note value = "aController")
was different from my xml configuration where i was instatiating the same controller with different bean id
<bean id="aControleRRRRR" class="package.ControllerClassName"
p:property-ref="beanToInject" />
(Note id="aControleRRRRR")
So in conclusion your #Controller name (value = "aController") needs to be exactly the same as the name you give in the XML configuration (id="aControleRRRRR"), so that Spring can manage to distinct that they refer to the same bean (instance)
Hope this helps
I'm trying to setup mocked object (using Mockito) via spring bean configuration, however I don't know how to setup MockSettings for that object. Especially I would like to set up object serializable.
Programatically it is possible by:
Object serializableMock = mock(Object.class, withSettings().serializable());
<bean id="object" name="object" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="object" />
<constructor-arg value="org.mockito.MockSettings"> ??? </constructor-arg>
</bean>
Can somebody guide me how to do that ? Thanks in advance
BTW : I want to use pure XML configuration.
Only via XML, you can't.
Actually I wouldn't recommend you to have the usual practice of using Mockito mocks in spring for tests. Here's why :
Usually when Unit Testing, you want to test one class in isolation, it's unit testing after all, so Spring DI isn't necessary at all in this case. You just inject the collaborators of your test subject yourself or maybe via the handy #InjectMock annotation.
If however you need to test things with another system like a DAO with a Database, then indeed you probably need Spring wiring to connect to either the real DB or some in memory DB like H2. But in this case you are crafting an Integration Test. And you most probably don't need mocks in this case.
That said, you might have specific needs and the above point could be irrelevant in your specific bounded context. But then again, in my opinion if it's specific I don't think it's overkill to craft yourself a simple MockSettings factory bean (that could even be configurable).
E.g. you could write this once and for all in a technical module of your application :
public class SpringMockSettingsFactoryBean extends AbstractFactoryBean<MockSettings> {
#Override public Class<Multimap> getObjectType() {
return MockSettings.class;
}
#Override protected Multimap<String, String> createInstance() throws Exception {
// ... your code
return mockSettings;
}
}
There's a project springockito on bitbucket that tries to have a mockito focused namespace in spring. I don't think the project can do that, but the framework's author might be interested to implement the feature.
Hope that helps.
yes, you can do it via XML
<bean id="mockSettings" class="org.mockito.Mockito" factory-method="withSettings" init- method="verboseLogging">
</bean>
<bean id="sqsHelper" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.elsevier.vtw.aws.helper.SQSHelper"/>
<constructor-arg ref="mockSettings" />
</bean>
I am using Spring3 with xml based configuration.
The problem is when the IOC container starts it loads/caches all the properties/fields defined in com.dao.MyDAOFactory class. I want to tell spring that only load/cache specific properties/fields.
The bean declaration is given below
<bean id="daoFactory" class="com.dao.MyDAOFactory" ></bean>
Can any one help me ?
You can use the lazy-init attribute to defer the loading of your beans, but eventually all of them will be loaded.
Also keep this in mind that if a non-lazy singleton bean depends on one or more lazy beans, the lazy beans will be loaded at startup.
To be exact, how can I get the value of the <display-name> tag under the <web-app> tag stored in an application's web.xml in a Spring application context configuration XML file.
Ideally I would like something like the following...
<bean><property value="${servletContext.servletContextName}/></bean>
It seem like ServletContext.getServletContextName() does what I want but I can't seem to figure out how to get a handle on that in the Spring application context file.
Ok, the answer is trivial in Spring 3.0.x. Per the documentation for ServletContextFactory
Deprecated. as of Spring 3.0, since "servletContext" is now available as a default bean in every WebApplicationContext
So I decided to try the following and it worked!
<bean><property value="#{servletContext.servletContextName}/></bean>
Since servletContext object is implicitly defined we can access it via Spring EL using the #{} syntax.
I don't think you can do this via the XML config.
You can autowire a bean to receive the ServletContext object (or implement ServletContextAware), and fetch it from that programmatically, but I don't think the XML expressions have any visibility of it.
Maybe try the Expression Language?
<bean>
<property value="#{T(javax.servlet.ServletContext).getServletContextName()}"/>
</bean>
I suspect that would print null if it works though, since there is no context yet.
Spring Annotations - I am getting some documents regarding Annotations but, they are explaining each annotation and how to use it.
but i want, know how can achieve same annotations behavior with bean configuration.
ex:
Annotation Bean config
#Required ?
Can you help.................
I don't really understand your question, but here are some links from the Spring Reference that seem relevant:
3.9. Annotation-based container configuration
3.11. Java-based container configuration
On second thought (and after editing your question's source) I seem to understand. I think you want an XML alternative to the #Required annotation.
Quote from the #Required section:
The container throws an exception if the affected bean property has not been populated; this allows for eager and explicit failure, avoiding NullPointerExceptions or the like later on.
I'm not sure such a thing exists in XML, I think the only way to get that behavior is through explicit wiring.
<bean class"foo.bar.Service.class">
<!-- This will fail if no bean named subService is available -->
<property name="subService" ref="subService" />
</bean>