从头认识Spring-2.6 在注解中使用表达式@Value(1)-注入普通值

这一章节我们来讨论一下在注解中使用表达式@Value注入普通值。

1.domain

厨师类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_17;

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

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.测试类:

package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_17;

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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
		"/com/raylee/my_new_spring/my_new_spring/ch02/topic_1_17/ApplicationContext-test.xml" })
public class ChiefTest {

	@Autowired
	private ApplicationContext applicationContext;

	@Test
	public void testChief() {
		Chief jack = applicationContext.getBean(Chief.class);
		System.out.println(jack.getName());
	}
}

3.配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

	<bean id="jack"
		class="com.raylee.my_new_spring.my_new_spring.ch02.topic_1_17.Chief" />
</beans>

配置文件这里只需要注册一个bean即可

测试输出:

jack

总结:这一章节主要介绍了在注解中使用@Value注入普通值。

目录:http://blog.csdn.net/raylee2007/article/details/50611627

我的github:https://github.com/raylee2015/my_new_spring

时间: 2024-08-23 00:17:22

从头认识Spring-2.6 在注解中使用表达式@Value(1)-注入普通值的相关文章

从头认识Spring-2.6 在注解中使用表达式@Value(2)-使用表达式注入其他对象的值

这一章节我们来讨论一下在注解中使用表达式@Value,使用表达式注入其他对象的值. 1.domain 蛋糕类:只有一个public的名称 package com.raylee.my_new_spring.my_new_spring.ch02.topic_1_18; import org.springframework.beans.factory.annotation.Value; public class Cake { @Value("blueberry cheese cake") p

spring学习2:基于注解+xml实现ioc和依赖注入

spring学习2:基于注解+xml实现ioc和依赖注入 一.在spring配置文件中开启spring对注解的支持 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&qu

spring data jpa @Query注解中delete语句报错

项目中需要删除掉表中的一些数据 @Query("delete from EngineerServices es where es.engineerId = ?1") int deleteByEgId(String engineerId); 但是提示了错误 org.hibernate.hql.QueryExecutionRequestException: Not supported for DML operations 通过查阅相关的资料发现,对于执行update和delete语句需要添

spring+hibernate实体类注解详解(非原创) + cascade属性取值

@Entity //继承策略.另一个类继承本类,那么本类里的属性应用到另一个类中 @Inheritance(strategy = InheritanceType.JOINED ) @Table(name="INFOM_TESTRESULT") public class TestResult extends IdEntity{} 1 @Entity(name="EntityName") 必须 name为可选,对应数据库中一的个表 2 @Table(name="

Spring系列【08】为JavaBean的集合对象注入属性值

TestUtil.java 1 package com.lh.util; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 7 public class TestUtil { 8 @Override 9 public String toString() { 10 return "TestUtil [list=" + list + ", set=" + set +

Spring学习笔记--使用注解装配

使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的context命名空间配置中的<context:annotation-config>元素,如下所示: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.

玩转SSH之Spring(一)---关于Srping框架中使用@query注解实现复杂查询

在Spring框架中,关于从数据表获取数据有不同的方法,当数据查询比较简单时,可以通过继承JpaRepository<T, L> 使用findBy***方法,通过分析方法名来实现查询,T表示要查询的数据表所对应的实体,L表示该实体主键的类型,比如Long.关于findBy方法如何通过分析方法名来实现查询,网上资料很多,不做赘述. 如果查询的数据比较复杂,查询条件比较复杂时,可以考虑使用JPA的@query方法来实现查询.关于使用方法,下面做简单介绍: 1.首先Dao层需继承Repository

spring注解中@component是什么意思

@Component("userManager") public class UserManagerImpl implements UserManager { private UserDao userDao; public UserDao getUserDao() { return userDao; } @Resource public void setUserDao(UserDao userDao) { this.userDao = userDao; } /* * (non-Java

Spring XML配置--使用注解装配(@Atutowired、@Inject、@Resource)

陈科肇--http://blog.csdn.net/u013474104/article/details/44352765 ======= 1.装配术语 创建应用对象之间协作关系的行为通常被称为装配 2.使用注解装配 Spring是从Spring2.5开始引入使用注解自动装配的. Spring容器是默认禁用注解装配的,因此如果要使用Spring的注解装配,你必须启用它.启用方式:使用Spring的context命名空间配置中的<context:annotation-config>元素,配置启用