Why ConfigurationProperties are not validated? - spring

What is difference between #Value and #ConfigurationProperties injection? Why Value properties are validated and ConfigurationProperties are not?

#Value is a core container feature and it does not provide the same features as type-safe Configuration Properties. It's maybe for that... I think. I currently work (discover) on spring (MVC) and I recommend you to take a look at the spring reference (Externalized Configuration)

Related

spring application - both #Configuration and Spring <beans> XML in the same application?

Is it possible to have a Spring application with both Spring XML that defines some of the beans and also a #Configuration class that defines other beans?
Yes, it's possible.
Spring Boot and Spring 5.x will drive you away from XML and towards annotations. It's a good idea to follow the trend and write using the proper idiom.
None of my Spring Boot apps use XML. That was part of Rod Johnson's original vision for Interface 21/Spring, but annotations have won the day.
It is certainly possible, but it is not recommended. Bean configuration should be consistent to be either all in #Annotations or in XML files.
Is there any specific reason that you want to put some bean configuration in XML rather than in #Configuration class?

Equivalent of #ImplementedBy in Spring

What is the equivalent of Guice's #ImplementedBy annotation in Spring DI? (I googled it but with no results.)
There is not exist JIT default binding in Spring. You can set only one implementation to dependency or use naming qualifier #Named or #Qualifier annotation to specify implementation but this is static binding (not equivalent to #ImplementedBy Guice implementation).
I created a spring extension enabling the jit-binding. this library add #ImplementedBy annotation to Spring. See https://github.com/devacfr/spring-implementedby and give me feedback
You can use the annotation #Qualifier
http://static.springsource.org/spring-framework/docs/3.2.0.M2/api/org/springframework/beans/factory/annotation/Qualifier.html

create beans with annotation spring

In struts2 i almost did not use any xml configs and used much of annotations for MVC. I build a small application in that way using struts2. Now i want to develop same project using spring 3.2. Now i want to use annotation to create beans and request mapping (this i used). I want a clear example of using bean annotations and is it possible to set properties using annotations. I am getting confused because the documentation is too large, and many annotations. providing a simple list of annotations and their usage with simple example will be a great help.
Iam doing sample project on Spring 3.1.
I have used some annotations to create beans.Below are the annotations i have used.
#Component - Annotation used to create a bean given by Spring
#Resource,#Bean
JSR Annotations: #Controller,#Repository, #Service
If you are annotating your class with above annotations Spring Container will create beans for you.
Your properties will be set with help of #Autowired annotation.

Hard?: Spring security on classes that are not Spring Beans?

Definitely need some expert help with this! I think this is mainly a Spring Security question, but as I don't know for sure, so I am also tagging with the general Spring tag!
When the Application Context is loaded (mine is all via Java Config, though I don't believe that matters), the "DefaultListableBeanFactory" is processed and eventually (via the ProxyFactory) Spring Security Advisors are added. This is great when I have Spring Beans as I have Permissions that need authorization.
My question is: how do I get the same effect when I no longer require those classes to be Spring Beans? Said differently, if I have an object instance created as a singleton bean via Java Config and the authorization is working correctly, is it possible to maintain that with the object instance being a POJO? Again, for the experts, I want the interception chain returned in the JdkDynamicAopProxy to contain the Spring Security interceptors.
And "no", I am not really expecting an answer to this, maybe just hoping!!!
To add security interceptors to beans not instantiated by spring container
switch global-security tag to mode aspectj and weave the provided AnnotationSecurityAspect in the aspecj module.
For your second question I suppose that you want to do one of the following:
Use a ProxyFactoryBean to secure a bean.
Create security proxies programmatically: Use ProxyFactory.addAdvice() method.
Add the security interceptor to all proxies created by an AutoProxyCreator: This usually don't needed, but you can use the AbstractAutoProxyCreator.interceptorNames property to add common interceptors. The global-security tag parser uses a generated name for the MethodSecurityInterceptor, so you need to configure the interceptor manually and set a consistent SecurityMetadataSource.

Spring framework default-lazy-init all beans

Is there a spring property to lazy-init all beans that spring framework loads ?
I know about these properties
- lazy-init="true"
- default-lazy-init="true"
however there are multiple spring config xml files and some are packaged within jar so dont have liberty to change neither <bean> nor <beans> tag.
Any other way to tackle this via configuration ? or programatically ?
Short of extending the Spring bean loader, none that I know of.
You caN also use #Lazy annotation, but it is the same as you mentioned above.
According to java doc this should work ( though it looks not nice)
if (context.getBeanFactory() instanceof DefaultListableBeanFactory)
{
((DefaultListableBeanFactory) context.getBeanFactory()).setAllowEagerClassLoading(false);
}
I've implemented this on my company, had to extend some classes of spring tough. It wasn't easy, but we gained about 20s on every tomcat startup. Unfortunately, for privacy clauses, I can't show the code, but take a look at ClassPathBeanDefinitionScanner,DefaultBeanDefinitionDocumentReader,ContextNamespaceHandler and ComponentScanBeanDefinitionParser classes.
Starting with Spring Boot 2 you can use the spring.main.lazy-initialization property to configure lazy initialization across the whole application.
Setting the property value to true means that all the beans in the application will use lazy initialization.
in application.yaml
spring:
main:
lazy-initialization: true
or in application.properties
spring.main.lazy-initialization=true

Resources