入門註解@Value

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

15 directories, 4 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springioc.domain;

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

@Component
public class User {

	// 容器在啓動的時候,會默認註冊一套spel的解析器,可以結合該註解來直接只用該表達式。
	private @Value("#{'hack'}") String name;

	public String getName() {
		return name;
	}

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

}
package ocn.site.springioc.setup;

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

@Configuration
@ComponentScan({ "ocn.site.springioc.domain" })
public class Appconfig {

}
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 {
		logger.info(context.getBean(User.class).getName());
	}

}
19-09-09 10:54:13 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:54:13 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:54:13 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Sep 09 10:54:13 CST 2019]; root of context hierarchy
19-09-09 10:54:13 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor  =====>>> JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
19-09-09 10:54:13 ocn.site.springioc.domain.Runtest  =====>>> hack
19-09-09 10:54:13 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@3a82f6ef: startup date [Mon Sep 09 10:54:13 CST 2019]; root of context hierarchy
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章