In this , u will see , how to get the values of .properties into java using spring annotations
      
    
 
  
  
in the spring application context xml file defined as below
    <util:properties id="pwdProperties"
        location="classpath:/PwdRules.properties" />
and define a properties file with the name PwdRules.properties, add any name value pair to this file
let consider we added like below
name=rby
now define a class , with the name . AppConfig.java , inside , write the below code
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
@Configuration
//spring config that loads the properties file
@ImportResource("classpath:/login-connector.xml")
public class AppConfig {
 /**
     * Using property 'EL' syntax to load values from the
     * jetProperties value
     */
    private @Value("#{pwdProperties['
name  ']}") String 
name  ;
    /**
     * Create a jetBean within the Spring Application Context
     * @return a bean
     */
    public @Bean(name = "pwdBean")
    PwdBean pwdBean() {
     PwdBean bean = new PwdBean();
        bean.setName(name);
        return bean;
    }
}
define a bean class with the name
PwdBean.java and add one member field for that as, name and generate setter and getter for that 
and u can write the test case as below to test this
ApplicationContext ctx =
            new AnnotationConfigApplicationContext(AppConfig.class);
  PwdBean bean = ctx.getBean(PwdBean.class);
  System.out.print(bean.getPwdrule());
No comments:
Post a Comment