前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >spring的XML装配 原

spring的XML装配 原

作者头像
尚浩宇
发布2018-08-17 10:34:06
1830
发布2018-08-17 10:34:06
举报
文章被收录于专栏:杂烩杂烩

贴上几个类,留作备忘

代码语言:javascript
复制
/**
 * <p>Description: </p>
 * @author scc
 * @since 创建时间:2015年11月2日 下午1:44:05
*/
package test.testdi;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

/**
 * <p>Description: </p>
 * @author scc
 * @since 创建时间:2015年11月2日 下午1:44:05
 */
public class Student {
	private Teacher teacher;
	private String name;
	private Toy toy;
	private List<String> list;
	private Set<Toy> toys;
	private Map<String, Toy> detailtoys;
	private Properties properties;
	/**
	 * <p>Description: </p>
	 * @author scc
	 * @since 创建时间:2015年11月2日 下午1:50:41
	 ********************************** 
	 */
	public Student() {
		// TODO Auto-generated constructor stub
	}
	public Student(Teacher teacher) {
		this.teacher=teacher;
	}
	
	public void init(){
		System.out.println("init");
	}
	public void say(){
		System.out.println((null==teacher?"":teacher.teach())+"say somthing");
	}
	public void say2(){
		System.out.println(toy.toy()+"========="+ name);
	}
	public void destory(){
		System.out.println("destory");
	}
	public void say3(){
		for (String string : list) {
			System.out.println(string);
		}
	}
	public void say4(){
		for (Toy toy : toys) {
			System.out.println(toy.toy());
		}
	}
	public void say5(){
		for(Entry<String, Toy> toy:detailtoys.entrySet()){
			System.out.println(toy.getKey()+"========"+toy.getValue().toy());
		}
	}
	public void say6(){
		for(Entry<Object, Object> toy:properties.entrySet()){
			System.out.println(toy.getKey()+"========"+toy.getValue());
		}
	}
	…………………省略setter和getter………………………
}

xml

代码语言:javascript
复制
<?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:aop="http://www.springframework.org/schema/aop"
             xmlns:tx="http://www.springframework.org/schema/tx"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-3.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
	<bean id="student" class="test.testdi.Student" init-method="init" destroy-method="destory">
		<constructor-arg index="0" ref="teacher"/>
		<property name="name" value="张三"/>
		<property name="toy" ref="toy"/>
		<property name="list">
			<list>
				<value>1</value>
				<value>1</value>
				<value>1</value>
				<value>1</value>
				<value>1</value>
			</list>
		</property>
		<property name="toys">
			<set value-type="test.testdi.Toy">
				<ref bean="toy"/>
				<ref bean="toy"/>
				<ref bean="toy"/>
			</set>
		</property>
		<property name="detailtoys">
			<map>
				<entry key="no1" value-ref="toy"/>
				<entry key="no2" value-ref="toy"/>
			</map>
		</property>
		<property name="properties">
			<props>
				<prop key="no1">haha</prop>
				<prop key="no2">heihei</prop>
			</props>
		</property>
	</bean>
	<bean id="teacher" class="test.testdi.Teacher"/>
	<bean id="toy" class="test.testdi.Toy"/>
	<bean id="log" class="test.testdi.Log"/>
	<aop:config>
		<aop:aspect ref="log">
			<aop:pointcut expression="execution(* *.say(..))" id="cutstudent"/>
			<aop:before method="before" pointcut-ref="cutstudent"/>
			<aop:after method="after" pointcut-ref="cutstudent"/>
		</aop:aspect>
	</aop:config>
</beans>

pom.xml

代码语言:javascript
复制
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>stu-spring</groupId>
	<artifactId>stu-spring</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<dependencies>
		<!-- spring基础 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>3.2.3.RELEASE</version>
		</dependency>
		<!-- spring切面 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>
	</dependencies>
</project>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015/11/02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档