contain an Environment You can create and register your own Environment And/or customize with your own PropertySources Using ConfigurableEnvironment API or with @PropertySource annotation
value="${db.driver}"/> <property name="url" value="${db.driver}"/> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean> <!-- in 3.1, ${...} placeholders may be --> <!-- from 'db.properties' as well as all --> <!-- property sources registered with the --> </beans> <!-- Environment -->
DataSource dataSource() { SimpleDriverDataSource ds = new SimpleDriverDataSource(); ds.setDriverClass(...); ds.setUrl(...); // where do these properties ds.setUsername(...); // come from? ds.setPassword(...); return ds; } }
Environment env; @Bean public DataSource dataSource() { SimpleDriverDataSource ds = new SimpleDriverDataSource(); ds.setDriverClass(env.getPropertyAsClass("db.driver")); ds.setUrl(env.getProperty("db.url")); ds.setUsername(env.getProperty("db.username")); ds.setPassword(env.getProperty("db.password")); return ds; // property values may be resolved from } // 'db.properties' or any other PropertySource } // registered with the Environment
<bean id="a" class="com.company.A"/> <bean id="b" class="com.company.B"/> <bean id="c" class="com.company.C"/> </beans> Main.java public static void main(String... args) { GenericXmlApplicationContext ctx = new GXAC(); ctx.load("classpath:com/company/*-config.xml"); ctx.refresh(); ctx.getBean("a"); // oops! "dev" profile not active, // so bean "a" is not found }
{ Foo foo = new Foo(); foo.setBar(bar()); return foo; } @Bean public Bar bar() { return new Bar(); } } ... new AnnotationConfigApplicationContext(AppConfig.class);
new LocalSessionFactoryBuilder(dataSource()) .addAnnotatedClasses(Person.class, Account.class) .buildSessionFactory(); } @Bean public PersistenceExceptionTranslator exTranslator() { return new HibernateExceptionTranslator(); }