<bean id="groupContainerRelationshipDAO" class="containermanager.management.dao.GroupContainerRelationshipDAOImpl">
<property name="dataSource">
<util:property-path path="dataSourceFactory.dataSource" />
</property>
</bean>
I would like to define this as spring annoations. The problem is dataSource is a memeber of the inherited class SimpleJdbcDaoSupport. Does someone knows how to do this?
This one:
#Component
class GroupContainerRelationshipDAOImpl {
#Value("#{dataSourceFactory.dataSource}")
private DataSource dataSource;
}
Read more about #Value annotation: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-annotation-config
Related
Config my custom type converters by using (Spring 4.x)XML properties like this.
<mvc:annotation-driven conversion-service="factoryBean" />
<bean class="org.springframework.context.support.ConversionServiceFactoryBean" id="factoryBean" >
<property name="converters">
<list>
<bean class="com.mvc.convertor.MyConvertor" />
</list>
</property>
</bean>
MyConvertor implements org.springframework.core.convert.converter.Converter.
And how can I config my custom type converters by using SpringBoot.I have tried many methods but failed.Hope any one can help me to resolve.Thanks!
As Stephane Nicoll pointed out spring boot should automatically pick up any converters registered as bean configuration in your application.
#Configuration
#EnableWebMvc
public class MyConfiguration {
#Bean
public HttpMessageConverters customConverters() {
HttpMessageConverter<?> additional = ...
HttpMessageConverter<?> another = ...
return new HttpMessageConverters(additional, another);
}
}
I am refering to Spring AOP by Mkyong http://www.mkyong.com/spring/spring-aop-example-pointcut-advisor/
It worked when tried running from main (i.e. App.java) as given on above link,
I want to integrate it in restful webservice where i have multiple service like CutomerService in mkyong's example.
For example i have controller which calls CustomerService,
#RestController
#RequestMapping(value = "/customer")
public class CustomerController{
#Autowired CustomerService customerService;
#RequestMapping(value = "/getCustomer", method = RequestMethod.GET")
public ResponseEntity<CommonResponse> getService(){
customerService.printName();
}
}
It didn't worked.
i have tried this also:
#Autowired
private ProxyFactoryBean customerServiceProxy;
#RequestMapping(value = "/getCustomer", method = RequestMethod.GET")
public ResponseEntity<CommonResponse> getService(){
CustomerService customerService = (CustomerService) customerServiceProxy
.getTargetSource().getTarget();
customerService.printName();
}
}
this dosent work either.
Any Solution for this?
my bean-config.xml is same as mkyong's example.
It worked ! Just need to change proxy class from "org.springframework.aop.framework.ProxyFactoryBean" to
"org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
Auto proxy creator identifies beans to proxy via a list of names. So we don,t need to specify target bean instead list of beans can be specified and it will intercept automatically.
My config look as below:
<bean id="adviceAround" class="com.tdg.ess.semantic.event.log.AdviceAround" />
<!-- Event logging for Service -->
<bean id="testServicePointCut" class="org.springframework.aop.support.NameMatchMethodPointcut">
<property name="mappedNames">
<list>
<value>process</value>
</list>
</property>
</bean>
<bean id="testServiceAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="pointcut" ref="testServicePointCut" />
<property name="advice" ref="adviceAround" />
</bean>
<bean
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="beanNames">
<list>
<!-- Differnt beans which have certain service defined like CustomerService-->
<value>testService1</value>
<value>testService2</value>
</list>
</property>
<property name="interceptorNames">
<list>
<value>testServiceAdvisor</value>
</list>
</property>
</bean>
Thanks everyone for looking into.
I have a spring boot application where I am trying to convert the following spring xml config to java config:
<bean id="pageDAO" factory-bean="springWSDaoFactory"
factory-method="createPageDAO" lazy-init="true">
<constructor-arg type="java.lang.String" value="${cds.host}" />
<constructor-arg type="java.lang.Integer" value="${cds.port}" />
</bean>
<!-- CoreApi + plugins configuration -->
<import resource="classpath:coreAPI_SpringWSContext.xml"/>
<bean name="springWSDaoFactory" class="com.blan.torque.dao.springws.SpringWSDAOFactory" lazy-init="true">
<property name="serviceVersion" value="${service.version}"/>
<property name="securityKey" value="${service.key}"/>
</bean>
Here's what I have for javaconfig so far:
#Bean
public PageDAO pageDAO() {
return springWSDAOFactory().createPageDAO(null, null);
}
#Bean
public SpringWSDAOFactory springWSDAOFactory() {
SpringWSDAOFactory springWSDAOFactory = new SpringWSDAOFactory();
springWSDAOFactory.setServiceVersion(null);
springWSDAOFactory.setSecurityKey(null);
return springWSDAOFactory;
}
I have no idea how to implement <import resource...../> in Java let alone import the variables like ${cds.host}. I've put null everywhere as place holders. But any ideas on how to do this with annotations?
In your configuration class you can use the annotation #ImportResource instead of <import resource... />.
To read Strings from a property file, try to declare an String using #Value and the use the previously declared string.
For example:
#Value("${service.version}")
private String serviceVersion;
Then using it as parameter
springWSDAOFactory.setServiceVersion(this.serviceVersion);
I hope have helped you.
For some reason i have my spring beans config as java config and also some other beans are defined in xml.
Now i want to inject the sessionfactory bean defined in xml config into a bean in java config.
Unfortunately sessionfactory is null and i get a NullPointerException.
What's wrong? I'm using Spring 3.2.5. Also #Inject or #Autowired instead of #Resource doesn't work.
Here are the relevant parts...
Java Config:
#Configuration
#ComponentScan
#ImportResource({ "classpath:beans-sessionfactory.xml" })
public class MyJavaConfig {
// defined in 'beans-sessionfactory.xml'
#Resource
//#Inject => also NPE
//#Autowired => also NPE
private AnnotationSessionFactoryBean sessionFactory;
#Bean
public MyDao getMyDao() {
final MyDao dao = new MyDao();
// Why is sessionFactory null?
dao.setSessionFactory(this.sessionFactory.getObject());
return dao;
}
// MyAppConfig extends PropertyPlaceholderConfigurer
#Bean
public MyAppConfig myAppConfig() {...}
...
XML-Config:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
...
</bean
Unit-Test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = { MyJavaConfig.class })
public class MyBeanWiringTest {
// test fails with NPE
}
The almost complete beans-sessionfactory.xml:
<?xml version="1.0" encoding="ISO-8859-15"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="true">...</bean>
<bean id="myProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">...</bean>
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">...</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="hibernateProperties" ref="hibernateProperties"/>
<property name="annotatedClasses">
<list>
<value>MyEntity</value>
</list>
</property>
</bean>
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
The AnnotationSessionFactoryBean is a FactoryBean<SessionFactory> which means that it creates a SessionFactory. You can inject the result instead of the factory.
#Configuration
#ComponentScan
#ImportResource({ "classpath:beans-sessionfactory.xml" })
public class MyJavaConfig {
#Autowired
private SessionFactory sessionFactory;
#Bean
public MyDao getMyDao() {
final MyDao dao = new MyDao();
// Why is sessionFactory null?
dao.setSessionFactory(this.sessionFactory);
return dao;
}
If you really want the AnnotationSessionFactoryBean you will have to resort to some naming trickery. You have to inject a bean with the name '&sessionFactory' notice the & this is the marker to indicate to spring that you want to FactoryBean instead of the result of the FactoryBean.
#Autowired
#Qualifier("&sessionFactory")
private AnnotationSessionFactoryBean sessionFactory;
The problem is here:
Bean method MyJavaConfig.myAppConfig is non-static and returns an
object assignable to Spring's BeanFactoryPostProcessor interface.
This will result in a failure to process annotations such as
#Autowired, #Resource and #PostConstruct within the method's declaring
#Configuration class. Add the 'static' modifier to this method to
avoid these container lifecycle issues; see #Bean Javadoc for complete
details
So after declaring myAppConfig static it works.
#Bean
public static MyAppConfig myAppConfig() {...}
here is some code from a post here that would explain my question:
Interface:
package org.better.place
public interface SuperDuperInterface{
public void saveWorld();
}
Implementation:
package org.better.place
import org.springframework.stereotype
public class SuperDuperClass implements SuperDuperInterface{
public void saveWorld(){
System.out.println("Done");
}
}
Client:
package org.better.place
import org.springframework.beans.factory.annotation.Autowire;
public class SuperDuperService{
private SuperDuperInterface superDuper;
public void doIt(){
superDuper.saveWorld();
}
public void setSuperDuper(SuperDuperInterface superDuper) {
this.superDuper = superDuper;
}
}
My question is - how can I configure the beans in the spring config? I don't want to use #Autowired and other annotations.
I guess it will be something like this:
<bean id="superService" class="org.better.place.SuperDuperService">
<property name="superDuper" ref="superWorker"
</bean>
<bean id="superWorker" class=?????? parent=???????? >
</bean>
You will have to instantiate the implementing class, of course:
<bean id="superWorker" class="org.better.place.SuperDuperClass"/>
You would only need the parent attribute if you wanted to create multiple beans with common properties that you don't want to repeatedly declare, so you move it to an abstract parent bean definition that the concrete bean definitions can reference.
Assuming the SuperDuperClass has some properties:
<bean id="superWorkerPrototype" abstract="true"
class="org.better.place.SuperDuperClass">
<property name="prop1" value="value1"/>
<property name="prop2" value="value2"/>
</bean>
<bean id="superWorker1" parent="superWorkerPrototype"
class="org.better.place.SuperDuperClass">
<property name="prop3" value="foo"/>
</bean>
<bean id="superWorker2" parent="superWorkerPrototype"
class="org.better.place.SuperDuperClass">
<property name="prop3" value="bar"/>
</bean>
Which would result in both instances having the same values for prop1 and prop2, but different ones for prop3.
You can just give the implementation class's fully qualified name, and its not required to give parent attribute. Spring will automatically find if it can assign an instance of SuperDuperClass to the superDuper field of SuperDuperService
<bean id="superWorker" class="org.better.place.SuperDuperClass" >
</bean>