入门注解@PropertySource

siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springioc
│   │               ├── domain
│   │               │   └── User.java
│   │               └── setup
│   │                   └── Appconfig.java
│   └── resources
│       └── sources
│           └── user.properties
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springioc
    │               └── domain
    │                   └── Runtest.java
    └── resources

16 directories, 5 files

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-contextartifactId>
    <version>4.3.22.RELEASEversion>
dependency>

<dependency>
    <groupId>log4jgroupId>
    <artifactId>log4jartifactId>
    <version>1.2.17version>
dependency>

<dependency>
    <groupId>junitgroupId>
    <artifactId>junitartifactId>
    <version>4.12version>
    <scope>testscope>
dependency>

<dependency>
    <groupId>org.springframeworkgroupId>
    <artifactId>spring-testartifactId>
    <version>4.3.22.RELEASEversion>
    <scope>testscope>
dependency>
package ocn.site.springioc.domain;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User {

	private @Value("${id}") int id;
	private @Value("${name}") String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}

}
package ocn.site.springioc.setup;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

//注解 @PropertySource
//可在 java-config 或 anno-config 风格的配置中,便捷的导入属性文件.
@Configuration
@ComponentScan({ "ocn.site.springioc.domain" })
@PropertySource("classpath:/sources/user.properties")
public class Appconfig {

}
id=34
name=hack
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import ocn.site.springioc.setup.Appconfig;

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = Appconfig.class)
public class Runtest {

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired ApplicationContext context;

	@Test
	public void run() throws Exception {
		User user = context.getBean(User.class);
		logger.info(user);
	}

}
19-09-09 10:19:31 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-09 10:19:31 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@3b81a1bc, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@64616ca2, org.springframework.test.context.support.DirtiesContextTestExecutionListener@13fee20c]
19-09-09 10:19:31 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Sep 09 10:19:31 CST 2019]; root of context hierarchy
19-09-09 10:19:31 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor  =====>>> JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
19-09-09 10:19:31 ocn.site.springioc.domain.Runtest  =====>>> User [id=34, name=hack]
19-09-09 10:19:31 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Sep 09 10:19:31 CST 2019]; root of context hierarchy

你可能感兴趣的:(入门注解@PropertySource)