注解、context和util的使用
@Autowired
@PostConstruct
@PreDestroy
@Test
以上这都是注解
cn.csdn.service. Address
package cn.csdn.service;
import java.util.List;
public class Address {
private List<String> list;
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}
cn.csdn.service. Emp
package cn.csdn.service;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.beans.factory.annotation.Autowired;
public class Emp {
private Address address;
public Address getAddress() {
return address;
}
@Autowired
public void setAddress(Address address) {
this.address = address;
}
@PostConstruct
public void init(){
System.out.println("初始化bean的操作...");
}
public Emp() {
System.out.println("初始化...");
}
@PreDestroy
public void distory(){
System.out.println("销毁的操作...");
}
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
<bean id="address" class="cn.csdn.service.Address">
<property name="list">
<util:list>
<value>kouxiaolin</value>
</util:list>
</property>
</bean>
<bean id="emp" class="cn.csdn.service.Emp"></bean>
<context:annotation-config/>
<!--
//生命周期的bean
<bean id="commonAnnotationBeanPostProcessor"
class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
//自动装配
bean <bean id="autowiredAnnotationBeanPostProcessor"
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
-->
</beans>
Context和util
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-2.5.xsd">
它们既可以手动配置也可以自动生成
自动生成:在配置文件视图编辑器,右击beans,再点击Editor namespace,出现对话框,点击Add,选择Specify new Namespace,点击Browse选择select xml Catalog entry,选择即可。
Prefix是前缀
Namespace name:命名空间
cn.csdn.service. App
package cn.csdn.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
@Test
public void test(){
ApplicationContext ac = new ClassPathXmlApplicationContext("app*.xml");
Emp emp = (Emp) ac.getBean("emp");
System.out.println(emp);
((AbstractApplicationContext)ac).close();
}
}
分享到:
相关推荐
Spring boot @Aspect简介使用场景列子1 (演示基本过程切点表达式切点复合运算切点匹配方法通知注解切面类执行顺序 简介 在 Spring实现 AOP面向切面编程, 是通过 @Aspect注解来实现切面的 使用场景 常见用于记录日志,...
Spring 2.0当中为缩减XML配置文件数量引入定制命名空间功能,从此它便深深植根于核心Spring框架(aop、context、jee、jms、 lang、tx和util命名空间)、Spring Portfolio项目(例如Spring Security)和非Spring项目...
注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --> <value>...
12.5.2. 使用 SqlMapClientTemplate 和 SqlMapClientDaoSupport 12.5.3. 基于原生的iBATIS API的DAO实现 12.6. JPA 12.6.1. 在Spring环境中建立JPA 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的...
12.5.2. 使用 SqlMapClientTemplate 和 SqlMapClientDaoSupport 12.5.3. 基于原生的iBATIS API的DAO实现 12.6. JPA 12.6.1. 在Spring环境中建立JPA 12.6.2. JpaTemplate 和 JpaDaoSupport 12.6.3. 基于原生的...
4.7. Application context 和Resource 路径 4.7.1. 构造application context 4.7.2. Application context构造器中资源路径的通配符 4.7.3. FileSystemResource 提示 5. 校验,数据绑定,BeanWrapper,与属性编辑...
4.7. Application context 和Resource 路径 4.7.1. 构造application context 4.7.1.1. 创建 ClassPathXmlApplicationContext 实例 - 简介 4.7.2. Application context构造器中资源路径的通配符 4.7.2.1. Ant风格的...
目录 前言 1. 简介 1.1. 概览 1.2. 使用场景 2. Spring 2.0 的新特性 ... 使用JDK 5.0注解 20.3.4. 源代码级的元数据类型 20.3.5. 接口AutodetectCapableMBeanInfoAssembler 20.3.6. 用Java接口定义管理接口 ...
本文主要介绍使用注解方式配置的spring mvc,之前写的spring3.0 mvc和rest小例子没有介绍到数据层的内容,现在这一篇补上。下面开始贴代码。 文中用的框架版本:spring 3,hibernate 3,没有的,自己上网下。 先说...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
1个目标文件,JNDI的使用例子,有源代码,可以下载参考,JNDI的使用,初始化Context,它是连接JNDI树的起始点,查找你要的对象,打印找到的对象,关闭Context…… ftp文件传输 2个目标文件,FTP的目标是:(1)提高...
MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个...
可以直接把一个PO类存到数据库通过PO类和一个id可以获取到该对象通过PO类可以直接update数据库记录不需要实现 BatchPreparedStatementSetter, 就可以批量update通过一个对PO对象删除对应的数据库记录依然可以使用...