How to change XML-based Configuration to annotation configuration in Spring? - spring

The class is as following:
class ReportControllerBase {
String reportName = "Report";
public String getReportName() {
return reportName;
}
public void setReportName(String reportName) {
this.reportName = reportName;
}
// ...
}
class AnnualReportController extends ReportControllerBase {
// ...
}
class SkinCareAnnualReprotController extends AnnualReportController {
String productName;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
}
And the XML-based configuration is as following:
<bean id="annualReportController" class="AnnualReportController">
<property name="reportName" value="Annual Report"/>
</bean>
<bean id="annualSpecialReportController" class="AnnualReportController">
<property name="reportName" value="Annual Special Report"/>
</bean>
<bean id="skinCareAnnualReprotController" class="SkinCareAnnualReprotController" parent="annualReportController">
<property name="productName" value="A famous skin care product"/>
</bean>
The Bean annualReportController and annualSpecialReportController is the instance of the same Class. And skinCareAnnualReprotController is inherited from bean annualReportController.
How to implement this configuration in annotation-based configuration in Spring?

#Configuration
public class ReportConfiguration {
#Bean public AnnualReportController annualReportController() {
AnnualReportController annualReportController = new AnnualReportController();
annualReportController.setReportName("Annual Report");
return annualReportController;
}
#Bean public AnnualReportController annualSpecialReportController() {
AnnualReportController annualSpecialReportController = new AnnualReportController();
return withAnnualSpecialReportName(annualSpecialReportController);
}
#Bean public SkinCareAnnualReportController skinCareAnnualReportController() {
SkinCareAnnualReportController skinCareAnnualReportController = new SkinCareAnnualReportController();
skinCareAnnualReportController.setProductName("A famous skin care product");
return withAnnualSpecialReportName(skinCareAnnualReportController);
}
// in this instance, a helper method is arguably overkill,
// but I've included it for demonstration
private <T extends AnnualReportController> T withAnnualSpecialReportName(T report) {
report.setReportName("Annual Special Report");
return report;
}
}
Take a look at the Spring 3.1 documentation for more information.

Related

How to inject bean in a Tapestry service

I want to inject bean in a Tapestry service (not in a page).
For the moment, I use this :
public class EntityRealm extends AuthorizingRealm {
ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-context-security.xml");
SecurityServices securityServices = (SecurityServices)ctx.getBean("securityServices");
It works, but I want use this :
public class EntityRealm extends AuthorizingRealm {
#Inject
private SecurityServices securityServices;
And my applicationContext is in the web.xml.
In this second case, the injection doesn't work. Why ?
AppModule.java :
public class AppModule
{
//#Resource(name = "realm")
#Inject
private static EntityRealm realm;
#Contribute(WebSecurityManager.class)
public static void addRealms(Configuration<EntityRealm> configuration) {
//EntityRealm realm = new EntityRealm();
configuration.add(realm);
}
public static void contributeFactoryDefaults( MappedConfiguration<String, Object> configuration)
{
configuration.override(SecuritySymbols.LOGIN_URL, "/login");
configuration.override(SecuritySymbols.UNAUTHORIZED_URL, "/login");
configuration.override(SecuritySymbols.SUCCESS_URL, "/index");
configuration.override(SymbolConstants.APPLICATION_VERSION, "2.0-SNAPSHOT");
}
public static void contributeApplicationDefaults(MappedConfiguration<String, Object> configuration)
{
configuration.add(SymbolConstants.HMAC_PASSPHRASE, new BigInteger(130, new SecureRandom()).toString(32));
configuration.add(SymbolConstants.SUPPORTED_LOCALES, "en,fr");
configuration.add( "tapestry.default-cookie-max-age", "31536000" );
}
public RequestFilter buildTimingFilter(final Logger log)
{
return new RequestFilter()
{
public boolean service(Request request, Response response, RequestHandler handler)
throws IOException
{
long startTime = System.currentTimeMillis();
try
{
return handler.service(request, response);
} finally
{
long elapsed = System.currentTimeMillis() - startTime;
log.info(String.format("Request time: %d ms", elapsed));
}
}
};
}
public void contributeRequestHandler(OrderedConfiguration<RequestFilter> configuration,
#Local
RequestFilter filter)
{
configuration.add("Timing", filter);
}
}
And the EntityRealm.java :
public class EntityRealm extends AuthorizingRealm {
//***************************************
//************* Attributes *************
//***************************************
//ApplicationContext ctx = new ClassPathXmlApplicationContext("/application-context-security.xml");
//SecurityServices securityServices = (SecurityServices)ctx.getBean("securityServices");
//#Resource(name = "securityServices")
#Inject
private SecurityServices securityServices;
//***************************************
//************ Constructors *************
//***************************************
public EntityRealm() {
super(new MemoryConstrainedCacheManager());
setName("myapprealm");
setAuthenticationTokenClass(UsernamePasswordToken.class);
}
//***************************************
//********** Public Methods *************
//***************************************
#Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) throw new AuthorizationException("PrincipalCollection was null, which should not happen");
application-context.xml :
<bean id="realm" class="net.atos.m2m.telecom.ihm.services.EntityRealm">
<property name="securityServices" ref="securityServices"></property>
</bean>
<bean id="securityServices" class="net.atos.m2m.telecom.ihm.applicatif.services.security.impl.SecurityServicesImpl">
<property name="servicesTelSecu" ref="servicesTelSecu"></property>
<property name="converterSecDSPtoDTO" ref="converterSecDSPtoDTO"></property>
<property name="converterSecDTOtoDSP" ref="converterSecDTOtoDSP"></property>
</bean>
Can you help me ?
Thank you.
How i say in previous comment, if you create EntityRealm in this way .. new EntityRealm() the inject\autowire does not work.
You must define EntityRealm as bean .. XML or Annotation.
<bean id="entityRealm" class="package.EntityRealm"/>
<bean id="securityServices" class="package.SecurityServices"/>
You can use #Resource instead,
#Resource(name = "securityServices")
private SecurityServices securityServices;
And make sure that application-context-security.xml file is loaded by Spring.

UsernameTokenValidator Can not #Autowired Dao

I have a Spring-ws and i am using Apahce-wss4j for spring-ws authentication. I want to use my Dao class in my custom TokenValidator class. But there was an exception can not #Autowired my Dao class. Here is my code
applicationContext.xml
<bean id="myWssConfig" class="tr.com.xxx.services.MyWssConfig"/>
<bean id="kepDBDAO" class="tr.com.xxx.dao.KepDBDAOImpl"/>
<bean id="ssha" class="tr.com.xxx.utils.SSHA"/>
<bean id="memberStatusService" class="tr.com.xxx.services.MemberStatusServiceImpl"/>
<bean id="myUsernameTokenValidator" class="tr.com.xxx.services.MyUsernameTokenValidator">
<property name="kepDBDAO" ref="kepDBDAO"/>
</bean>
<sws:interceptors>
<bean class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="validationActions" value="UsernameToken"/>
<property name="validationCallbackHandler" ref="callbackHandler"/>
<property name="wssConfig">
<ref bean="myWssConfig"/>
</property>
</bean>
</sws:interceptors>
Here is MyWssConfig.java
#Component("myWssConfig")
public class MyWssConfig extends WSSConfig {
public MyWssConfig() {
setValidator(WSSecurityEngine.USERNAME_TOKEN, MyUsernameTokenValidator.class);
setRequiredPasswordType(WSConstants.PASSWORD_TEXT);
}
}
And here is MyUsernameTokenValidator.java
#Component
public class MyUsernameTokenValidator extends UsernameTokenValidator {
private static final Logger LOGGER = LoggerFactory
.getLogger(MyUsernameTokenValidator.class);
#Autowired
private KepDBDAO kepDBDAO;
#Transactional
protected void verifyPlaintextPassword(UsernameToken usernameToken, RequestData data) throws WSSecurityException {
if (usernameToken != null && usernameToken.getPassword() != null) {
byte[] saltValue = null;
kepDBDAO.getWsUsers("basvuru");
String hashPassword = null;
try {
hashPassword = SSHA.calculateSSHA256(saltValue, usernameToken.getPassword());
} catch (NoSuchAlgorithmException e) {
LOGGER.error(e.toString(), e);
} catch (IOException e) {
LOGGER.error(e.toString(), e);
}
usernameToken.setPassword(hashPassword);
super.verifyDigestPassword(usernameToken, data);
}
}
public KepDBDAO getKepDBDAO() {
return kepDBDAO;
}
public void setKepDBDAO(KepDBDAO kepDBDAO) {
this.kepDBDAO = kepDBDAO;
}
}
Couldn't #Autowired my KepDBDAO when I call webservice in SOAPUI.
Help me please.. THank you all guys.
Try this:
1. In applicationContext:
<context:component-scan base-package="tr.com.xxx.dao"/>
<context:component-scan base-package="package for MyUsernameTokenValidator"/>
remove these beans:
kepDBDAO, myUsernameTokenValidator
2. Remove setter and getter for KepDBDAO in MyUsernameTokenValidator
3. Make sure KepDBDAOImpl is marked as #Service
I solved my problem.
#Component("myWssConfig")
public class MyWssConfig extends WSSConfig {
#Autowired
private MyUsernameTokenValidator myUsernameTokenValidator;
//
#PostConstruct
public void myInit() {
setValidator(WSSecurityEngine.USERNAME_TOKEN, myUsernameTokenValidator);
setRequiredPasswordType(WSConstants.PASSWORD_TEXT);
}
}

Spring Data Neo4j - relationship properties on complex type class members

I have a class annotated with #RelationshipEntity.
This class contains of object defined by me with some integer values.
Is it possible somehow to define that members of the nested object will be saved as properties on the relationship?
Justyna.
Yes, but they should be converted to strings providing customized Spring converters. To avoid declaring a converter for each class you need to embed, you could extend a common interface (even an empty one, just to declare the converters).
The converters must be declared in the SDN configuration file as follows:
<bean id="conversionService"
class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="..."/>
<bean class="..."/>
</list>
</property>
</bean>
You should define two converters, one for converting objects to strings and the other for the opposite conversion from string to objects.
For example, using Gson:
final class ToStringConverterFactory implements ConverterFactory<MyClass, String> {
#Override
public <T extends String> Converter<MyClass, T> getConverter(Class<T> type) {
return new ToStringConverter(type);
}
private final class ToStringConverter<E extends MyClass, S extends String> implements Converter<E, S> {
private Class<S> stringType;
public ToStringConverter(Class<S> stringType) {
this.stringType = stringType;
}
#Override
public S convert(E source) {
if (source != null) {
return (S) new Gson().toJson(source);
} else {
return null;
}
}
}
}
final class ToObjectConverterFactory implements ConverterFactory<String, MyClass> {
#Override
public <T extends MyClass> Converter<String, T> getConverter(Class<T> type) {
return new ToObjectConverter(type);
}
private final class ToObjectConverter<S extends String, E extends MyClass> implements Converter<S, E> {
private Class<E> objectType;
public ToObjectConverter(Class<E> objectType) {
this.objectType = objectType;
}
#Override
public E convert(S source) {
if (source != null) {
return (E) new Gson().fromJson(source, objectType);
} else {
return null;
}
}
}
}

PropertyPlaceholderConfigurer: Use external properties file

How to configure PropertyPlaceholderConfigurer to use properties files relative (some directories up) to the war?
We have running a war multiple times and each war should read its configuration for example from ../../etc/db.properties.
Update:
Yes, the properties files are outside the war. The directory structure is:
/htdocs/shop/live/apache-tomat/webapps/shop.war
should read
/htdocs/shop/live/etc/db.properties
and
/htdocs/shop/test/apache-tomat/webapps/shop.war
should read
/htdocs/shop/test/etc/db.properties
Finally, we have introduced a new resource type "relative:":
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:db.properties</value>
<value>relative:../../../etc/db.properties</value>
</list>
</property>
</bean>
We have extended XmlWebApplicationContext to inject custom resource handling:
public class Context extends XmlWebApplicationContext {
#Override
public Resource getResource(String location) {
if (location.startsWith(RelativeResource.RELATIVE_URL_PREFIX)) {
String relativePath = location.substring(RelativeResource.RELATIVE_URL_PREFIX.length());
return new RelativeResource(getServletContext(), relativePath);
}
return super.getResource(location);
}
}
Here is the relative resource class:
public class RelativeResource extends AbstractResource {
public static final String RELATIVE_URL_PREFIX = "relative:";
private final ServletContext servletContext;
private final String relativePath;
public RelativeResource(ServletContext servletContext, String relativePath) {
this.servletContext = servletContext;
this.relativePath = relativePath;
}
#Override
public String getDescription() {
return "RelativeResource [" + relativePath + "]";
}
#Override
public boolean isReadable() {
return true;
}
#Override
public boolean isOpen() {
return true;
}
#Override
public InputStream getInputStream() throws IOException {
String rootPath = WebUtils.getRealPath(servletContext, "/");
if (!rootPath.endsWith(File.separator)) rootPath += File.separator;
String path = rootPath + relativePath;
return new FileInputStream(path);
}
}
My code, based on mazatwork solution:
public class RelativeResource extends AbstractResource {
private final String relativePath;
public RelativeResource(String relativePath) {
this.relativePath = relativePath;
}
#Override
public String getDescription() {
return "RelativeResource [" + relativePath + "]";
}
#Override
public boolean isReadable() {
File resourceFile = new File(getAbsoluteFileLocation());
return resourceFile.exists();
}
#Override
public boolean isOpen() {
return true;
}
#Override
public InputStream getInputStream() throws IOException {
return new FileInputStream(getAbsoluteFileLocation());
}
private String getAbsoluteFileLocation() {
return Paths.get("").toAbsolutePath().resolve(relativePath).toString();
}
}
After that we can write in xml for example:
<bean id="configurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:application.properties</value>
<value type="com.blabla.RelativeResource">overrideProperties.properties</value>
</list>
</property>
<property name="ignoreResourceNotFound" value="true"/>
</bean>
Pros of this method - you don't hack Spring Context and don't stick to this hacked context implementation, you can use any (for example, not XmlWebApplicationContext, but ClassPathXmlApplicationContext) from Spring distribution.
In your configuration you can specify the properties from the classpath instead of relative to the configuration file.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
For this to work you must make sure that the properties file makes it to the classpath.
Somehow I wasn't able to get the desired path following others' methods, so here is my working version, based primarily on Dmitry's answer (usage in xml is identical), while isReadable() and getInputStream() looks more like mazatwork's version:
public class RelativeResource extends AbstractResource {
private final String relativePath;
public RelativeResource(String relativePath) {
this.relativePath = relativePath;
}
#Override
public String getDescription() {
return "RelativeResource [" + relativePath + "]";
}
#Override
public boolean isReadable() {
return true;
}
#Override
public boolean isOpen() {
return true;
}
#Override
public InputStream getInputStream() throws IOException {
String rootPath = this.getClass().getResource("/").getPath();
rootPath = URLDecoder.decode(rootPath, "UTF-8");
if (!rootPath.endsWith(File.separator)) rootPath += File.separator;
String path = rootPath + relativePath;
return new FileInputStream(path);
}
}

Spring #Cacheable Not Working

I have a dao method annotate with #Cacheable but its cache not working at all. I put log message inside the method.
<cache:annotation-driven mode="proxy" proxy-target-class="true" cache-manager="cacheManager" />
<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="WEB-INF/ehcache/ehcache.xml"></property>
<property name="shared" value="true"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache"></property>
</bean>
#Controller
#RequestMapping(value = "/analytics")
public class AnalyticsController {
#Autowired
private ReportDao reportDao;
/**
*
*/
public AnalyticsController() {
}
#RequestMapping(value = "/lcr-report", method = RequestMethod.GET)
public String viewCostReport(ModelMap map) {
List<Country> countryList = reportDao.getAllCountry();
map.put("countryList", countryList);
return "lcrReport";
}
}
#Repository
#Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT,
rollbackFor={DataAccessException.class, SQLException.class, Exception.class})
public class ReportDao {
#Autowired
private JdbcTemplate dao;
/**
*
*/
public ReportDao() {
}
#Cacheable(value = {"reportDao"}/*, key= "T(Country).hash(#List<Country>)"*/)
#Transactional(propagation=Propagation.REQUIRED, isolation=Isolation.DEFAULT, readOnly=true,
rollbackFor={DataAccessException.class, SQLException.class, Exception.class})
public List<Country> getAllCountry() {
List<Country> countryList = null;
BeanPropertyRowMapper<Country> mapper = new BeanPropertyRowMapper<Country>(Country.class);
PreparedStatementCreator psc = new GenericPreparedStatementCreator("select c.country_code as countryCode, c.name as countryName from country c");
System.out.println("Not from cache");
countryList = dao.query(psc, mapper);
return countryList;
}
}
You should create key by using parameters to method getAllCountry. In your case it is empty, so you can do like this:
#Transactional(readOnly = true)
#Cacheable(value = CACHE_NAME, key = "'countries'")
and check if it works using Map cache:
#Configuration
#EnableCaching(proxyTargetClass = true)
public class CacheProducer {
#Bean
public CacheManager cacheManager() {
SimpleCacheManager result = new SimpleCacheManager();
result.setCaches(Arrays.asList(new ConcurrentMapCache(DictionaryServiceImpl.CACHE_NAME)));
return result;
}
}
If it works - it is time to check your echache config.

Resources