Wednesday, June 3, 2015

Spring Security Annotations


@Resource :

If you want to use a Spring bean in some other child application or class
you can create a bean with a name and use annotation @Resource to use the defintion

@Bean(name = "user")
Public String HelloWorld(){
String str="Hello;
return str;

}

Now you can use this bean with name user and it would return a String Hello

@Resource(name="user"))
    String str;


Difference between @Qualifier and @Resource

@Autowired can be used alone . If it is used alone , it will be wired by type . So problems arises if more than one bean of the same type are declared in the container as @Autowired does not know which beans to use to inject. As a result , use @Qualifier together with @Autowired to clarify which beans to be actually wired by specifying the bean name (wired by name)
@Resource is wired by name too . So if @Autowired is used together with @Qualifier , it is the same as the @Resource.
The difference are that @Autowired and @Qualifier are the spring annotation while @Resource is the standard java annotation (from JSR-250) . Besides , @Resource only supports for fields and setter injection while @Autowired supports fields , setter ,constructors and multi-argument methods injection.
It is suggested to use @Resource for fields and setter injection. Stick with @Qualifier and@Autowired for constructor or a multi-argument method injection


To externalize Properties
PropertiesFactoryBean -- to store the info from a property file on a classpath/filesystem which has credentials.

PropertyPlaceholderConfigurer --  It pulls values from a properties file into bean definitions.

No comments:

Post a Comment