I cannot understand how to explicitly assign an ID to a repository bean in Spring Data.
To be clear, suppose I defined the following interface in package com.acme.repositories
public interface MyJpaRepo extends JpaRespository<MyEntity, Long> {
}
As per documentation I can activate Spring Data JPA repositories simply by adding
<jpa:repositories base-package="com.acme.repositories" />
Last, suppose that I have another bean in the same context which constructor requires a JpaRepository to be passed in. I would define it like that
<bean class="x.y.z.MyBean">
<constructor-arg name="repository" ref="XXX" />
</bean>
Now, what should I use in place of XXX? Or, even better, how can I explicitly assign an ID to the MyJpaRepo repository beans?
Thanks!
In place of XXX use myJpaRepo.
source: Spring Data Jpa document reference for repository id
<bean class="x.y.z.MyBean">
<constructor-arg name="repository" ref="<with qualified path>MyJpaRepo " />
</bean>
Related
I need to add OAuth client to a Spring 3 project. I need to use xml based configuration. And I want to know the xml equivalent of the following bean configuration I found in another Spring project. Note that there is an input parameter in the bean definition where an object of type OAuth2ClientContext is being passed (OAuth2ClientContext is an Interface) and is named clientContext. But no bean definition is written anywhere for clientContext. What does this mean? And how would you write this in xml?
#Bean
public OAuth2RestTemplate oauth2RestTemplate(OAuth2ClientContext clientContext){
return new OAuth2RestTemplate(oauth2Resource(), clientContext);
}
The configuration should be like this
<bean id="client" class="package.OAuth2ClientContext" />
<bean id="resource" class="package.Oauth2Resource" />
<bean id="restTemplate" class="package.Oauth2Resource">
<property name="nameOfPropertyResource" ref="resource" />
<property name="nameOfPropertyClient" ref="client" />
</bean>
are you sure that the bean client is not declared? Maybe it’s declared in some jar? If yes you should find it’s name and use the name in the ref
I'm having some problems understanding how to use annotations, especially with beans.
I have one component
#Component
public class CommonJMSProducer
And I want to use it in another class and i thought I could do that to have a unique object
public class ArjelMessageSenderThread extends Thread {
#Inject
CommonJMSProducer commonJMSProducer;
but commonJMSProducer is null.
In my appContext.xml I have this :
<context:component-scan base-package="com.carnot.amm" />
Thanks
You have to configure Spring to use this autowiring feature:
<context:annotation-config/>
You can find the details of annotation-based config here.
ArjelMessageSenderThread also have to be managed by Spring otherwise it won't tamper with its members since it does not know about it.
OR
if you cannot make it a managed bean then you can do something like this:
ApplicationContext ctx = ...
ArjelMessageSenderThread someBeanNotCreatedBySpring = ...
ctx.getAutowireCapableBeanFactory().autowireBeanProperties(
someBeanNotCreatedBySpring,
AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, true);
OR
as others pointed out you can use annotations to use dependency injection on objects which are not created by Spring with the #Configurable annotation.
It depends on how you create instances of ArjelMessageSenderThread.
If ArjelMessageSenderThread is a bean that should be created by spring you just have to add #Component (and make sure the package is picked up by the component scan).
However, since you extend Thread, I don't think this should be a standard Spring bean. If you create instances of ArjelMessageSenderThread yourself by using new you should add the #Configurable annotation to ArjelMessageSenderThread. With #Configurable dependencies will be injected even if the instance is not created by Spring. See the documentation of #Configurable for more details and make sure you enabled load time weaving.
I used XML instead of annotations. This seemed difficult for not a big thing. Currently, I just have this more in the xml
<bean id="jmsFactoryCoffre" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<constructor-arg name="brokerURL" type="java.lang.String"
value="${brokerURL-coffre}" />
</bean>
<bean id="jmsTemplateCoffre" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactoryCoffre" />
</property>
</bean>
<bean id="commonJMSProducer"
class="com.carnot.CommonJMSProducer">
<property name="jmsTemplate" ref="jmsTemplateCoffre" />
</bean>
And another class to get the bean
#Component
public class ApplicationContextUtils implements ApplicationContextAware {
Thanks anyway
I am developing a project and using 3rd party libraries. Let's say I use a library which gives me the object ExtObj. In my project I have a class MyObj, which uses ExtObj. How can I configure spring 3 to inject ExtObj in MyObj?
I tried to research the topic on the internet, but I didn't find a straight answer. I would like to use xml configuration and maybe (?) #Autowired, not #EJB or #Inject
Thanks in advance!
UPDATE
my guess was:
<bean id="myObj" value="me.MyObj">
<property name="extObj" value=" ... ??? ...">
</bean>
So, I don't know what I should put into value. I guess that's where the reference to the external object goes. But spring can only reference objects that have been already defined/configured in spring. So:
<bean id="extObj" value="ext.lib.ExtObj">
<bean id="myObj" value="me.MyObj">
<property name="extObj" value="extObj">
</bean>
Is that configuration right?
First you need to define a bean for your ExtObj in your application context (an xml file or a #Configuration class). Eg. if ExtObj has a constructor taking a String you can define the bean this way:
<bean id="extObj" class="ext.lib.ExtObj">
<constructor-arg value="SomeString"/>
</bean>
To define MyObj you can use constructor injection:
<bean id="myObj" class="me.MyObj">
<constructor-arg ref="extObj"/>
</bean>
or setter injection:
<bean name="myObj" class="me.MyObj">
<property name="extObj" ref="extObj"/>
</beans>
If you use setter injection then MyObj needs to have a setter setExtObj. If you use constructor injection MyObj needs to have a constructor taking an instance of the ExtObj class.
Of course you can inject a 3rd party library, as long as it has constructors that Spring can access.
You can use either XML or annotations - your choice.
You need to ask Spring to instantiate the instance(s) of the library class and then inject that into your objects that need them.
You do this every time you create a Spring data source that uses a JDBC driver. That's a 3rd party library.
In all the examples of autowiring that I have found, the example is about one <bean> autowire attribute which is set for example to byName, and the has only one property value which is supposed to be set through autowiring.
My question is what if a <bean> has multiple properties that you want to set through autowiring? No one seems to explain that situation. Can someone explain if I can or if I should use autowire to set multiple properties in a bean? The following is an example of such a situation where I want to set the account and credit properties of the customer bean by autowiring:
<beans>
<bean name="customer" class="ultratech.com.Customer" autowire="byName">
<bean name="account"/>
<bean name="credit>
</beam>
<bean name="account" class="ultratech.com.Account"/>
<bean name="credit" class="ultratech.com.Credit"/>
</beans>
Also, please correct me if I'm wrong, but if I were to use annotation (#Autowire), then my problem is easily solved, since I would be able to add #Autowire to any property of a bean separately.
[EDIT: edited to reflect on updated question]
Your question is much more clear right now. You seem to think (if I follow your thinking properly), that in the autowire="byName" you are supposed to provide a bean name instead of byName value.
That is not correct. The autowire attribute can take a few possible values and byName is one of those. When you set autowire to byName like here:
<bean name="someBean" class="foo.bar.Baz" autowire="byName />
then Spring will look at all the fields in someBean (foo.bar.Baz class) and attempt to wire all fields of this object on a per name basis. That is, (in your case) if a Customer class has a field account, Spring will look in its context and try to find a bean with name account to inject into the Customer bean.
If you define two such beans:
<bean name="customer" class="ultratech.com.Customer" autowire="byName" />
<bean name="account" class="ultratech.com.Account" />
then you are good to, if Customer is a class along this lines:
public class Customer {
(...)
private Account account;
(...)
}
Here is what your XML code snippet should look like, assuming that your Customer class has fields named account and credit:
<beans>
<bean name="customer" class="ultratech.com.Customer" autowire="byName" />
<bean name="account" class="ultratech.com.Account" />
<bean name="credit" class="ultratech.com.Credit" />
</beans>
Apart from "byName" autowiring, you can autowire:
no - default - no autowiring
byType - looks for a bean of the property type - be wary, though - only one bean of this type is allowed for autowiring byType; if there is more then one an exception is raised
constructor - works just like byType, but looks for constructor parameters only; all of constructor parameters have to be satisfied with exactly one bean of each respective type
See Spring reference for more info:
http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-factory-autowire
<context:annotation-config/>
<context:component-scan...
this is used for class that i need to annotated with #Repository #Service #Component...
<context:spring-configured />
<context:component-scan...
use if i need to use #Configurable
<tx:annotation-driven transaction-manager="transactionManager" />
<context:component-scan...
use if i need #Transactional , beside this what other metadata do I need to add in xml in order to use transaction management?
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
what is the need to add this in xml ? for what purpose?
<tx:annotation-driven transaction-manager="transactionManager" />
In order to use transaction management you also need to declare a transactionManager to use. That declaration depends on the approach you use to access the data. For example, for plain JDBC you write:
<bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name = "dataSource" ref = "dataSource" />
</bean>
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
This declaration is used to handle #PersistenceContext and #PersistenceUnit annotations for JPA data access configuration. Hovewer, support for this annotations also included in <context:annotation-config />, so you don't need to declare it explicitly if you use <context:annotation-config />.
From spring javadoc
BeanPostProcessor that processes PersistenceUnit and PersistenceContext annotations, for injection of the corresponding JPA resources EntityManagerFactory and EntityManager. Any such annotated fields or methods in any Spring-managed object will automatically be injected.
This post-processor will inject sub-interfaces of EntityManagerFactory and EntityManager if the annotated fields or methods are declared as such. The actual type will be verified early, with the exception of a shared ("transactional") EntityManager reference, where type mismatches might be detected as late as on the first actual invocation.