Spring注解中使用表达式@Value
2017-01-23 15:18
399 查看
blog主要包括两点内容:
1.注入普通值。
厨师类:
上面的代码中,我在name属性上面注入了名字jack的value
(2)配置文件:
配置文件这里只需要注册一个bean即可.
(3)测试类:
(4)输出:
可知通过@Value 注入普通值注入成功。
1.注入普通值。
1.使用@Value注解注入普通值
(1)entity厨师类:
package spring.ch2.topic6; import org.springframework.beans.factory.annotation.Value; /** * Created by louyuting on 17/1/22. */ public class Chief { @Value("jack") private String name = ""; public String getName() { return name; } public void setName(String name) { this.name = name; } }
上面的代码中,我在name属性上面注入了名字jack的value
(2)配置文件:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="jack" class="spring.ch2.topic6.Chief"> </bean> </beans>
配置文件这里只需要注册一个bean即可.
(3)测试类:
package spring.ch2.topic6; 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.SpringJUnit4ClassRunner; /** * Created by louyuting on 17/1/20. * 注入List和Set */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/spring/ch2/topic6/ApplicationContext-test.xml"}) public class ChiefTest { @Autowired private ApplicationContext applicationContext; @Test public void testChief(){ Chief jack = (Chief)applicationContext.getBean(Chief.class); System.out.println(jack.getName()); } }
(4)输出:
jack
可知通过@Value 注入普通值注入成功。
相关文章推荐
- 从头认识Spring-2.6 在注解中使用表达式@Value(2)-使用表达式注入其他对象的值
- 从头认识Spring-2.6 在注解中使用表达式@Value(1)-注入普通值
- 使用spring 的注解 @value使用方法
- 工作积累(五)——使用spring@Value注解实现常量功能
- Spring中Value注解的使用
- spring中使用@Scheduled注解时corn表达式、fixedRate、fixedDelay的区别
- Spring @Value注解的使用
- Spring3中@Value注解的使用
- 使用spring@Schedule注解定时任务时将时间表达式写入配置文件中的方法
- 关于spring使用@value注解获取properties文件属性问题
- 使用Spring 注解实现调度任务及cron表达式详解
- spring使用注解@value取properties时无法取到值
- 工作积累(五)——使用spring@Value注解实现常量功能
- Spring中Value注解的使用
- OA中总结:s:select,关于使用modelDriven,项目分层,@Transactional,jspf,各个层上配置注解交给spring管理的方法,简单的OGNL表达式写法
- Spring Boot使用Value注解给静态变量赋值的方法
- 使用 Spring 2.5 基于注解驱动的 Spring MVC
- Spring视频学习(九)使用Spring注解方式管理事务与传播行为详解
- 使用spring-modules-0.9实现注解缓存
- 使用 Spring 2.5 基于注解驱动的 Spring MVC