spring实战-声明,装配Bean(一)

本文介绍了Spring实战中的装配Bean,包括如何创建Spring配置,声明一个简单的Bean以及通过构造器进行注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.装配Bean

准备工作:我的开发环境是MyEclipse+tomcat,另外这里我使用了Maven,这样Maven可以自动帮我加载许多所依赖的jar包。
如何用MyEclipse创建一个工程并且支持maven:如图:
(1)将use defautlt location 打挑


(2)
(3)选择标准的maven项目工程,这里不使用myElicpse自己提供的


(4)不使用MyEclipse提供的依赖jar包


(5)在pom.xml中加载依赖,这里我们在加载依赖的时候可以去:点击打开链接http://mvnrepository.com/ 找到自己所需要的包,就可以,例如下面是我的pom.xml
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>Spring</groupId>
  <artifactId>Spring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <name>Spring</name>
  <description/>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  <dependencies>
  	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>jstl</artifactId>
		<version>1.2</version>zai
	</dependency>
  	<dependency>
		<groupId>javax.servlet.jsp</groupId>
		<artifactId>jsp-api</artifactId>
		<version>2.2</version>
	</dependency>
  	<dependency>
		<groupId>javax.servlet</groupId>
		<artifactId>javax.servlet-api</artifactId>
		<version>3.1.0</version>
	</dependency>
  	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-context</artifactId>
		<version>3.2.6.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-core</artifactId>
		<version>3.2.6.RELEASE</version>
	</dependency>
	<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-beans</artifactId>
		<version>3.2.6.RELEASE</version>
	</dependency>
	
  	
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.3.2</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.2</version>
        <configuration>
          <version>3.0</version>
          <failOnMissingWebXml>false</failOnMissingWebXml>
        </configuration>
      </plugin>
    </plugins>
  </build>
</project>
以上就是准备工作,下面是我在做试验的时候,根据大多数人的习惯,建立的项目目录结构:



1.1声明Bean:

比如,这里我们假设情景,有一个舞台,当然也需要表演人员表演,下面是接口代码,所有的表演人员都实现了这个接口:
package com;

import com.exception.PerformanceExcetion;

public interface Performer {

	void perform() throws PerformanceExcetion;
}


1.1.1创建Spring配置

从Spring3.0开始,Spring容器提供了两种配置Bean的方法,(1)使用一个或者多个XML文件作为配置文件(2)提供了基于java注解的配置方式,这里我们首先来说明使用xml实现,下面是一个典型的spring的XML配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	   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.0.xsd">
</beans>
在<beans>元素内,可以放置所有的Spring的配置信息,包括<bean>元素的声明。

1.1.2声明一个简单的Bean

我们的一个表演者是一位杂技师,实现了表演这个接口,如下:
package com.entity;

import com.Performer;
import com.exception.PerformanceExcetion;

public class Juggler implements Performer{
	
	private int beanBags=3;
	
	public Juggler(){
		
	}
	
	public Juggler (int beanBags){
		this.beanBags=beanBags;
	}

	@Override
	public void perform() throws PerformanceExcetion {
		System.out.println("Juggler(杂技师) "+ beanBags +" beanBags ");
		
	}

}

那么如何在sping配置文件中装配这个Bean呢?
 <bean id="juggler" class="com.entity.Juggler">
</bean>
接下来我们去测试这个类,让他可以执行:
public class springTest {
	public static void main(String[] args) {
		ApplicationContext cxt=new ClassPathXmlApplicationContext(
				 "classpath:/spring/spring.xml");
		Performer performer=(Performer) cxt.getBean("juggler");
		performer.perform();
		System.exit(0);
	}

}

此时输出:Juggler(杂技师) 3 beanBags 

1.1.3通过构造器注入


在定义我们的杂技师的时候,我们定义了一个构造器,就是想让他不止在扔3个东东,我们想传入参数,比如15个,这个时候我们可以在xml中这样去声明传入的参数:
<bean id="juggler" class="com.entity.Juggler">
	   		<constructor-arg value="15" />
 </bean>
这里我们通过构造器传入了一个参数,接下来我要通过构造器注入对象的引用:
这位杂技师不仅是一个杂技师,他还会吟诗呢,首先我们写一个吟诗的接口,使用对应的实现类实现这个接口会吟诵那些诗词,然后让我们这位杂技师通过构造函数就可以实现啦:
(1)诗词的接口:
public interface Poem {
	void recite();
}

(2)实现接口,会吟诵那首诗词:

public class poemImpl implements Poem{

	private static String context="轻轻的我走了,正如我轻轻的来";	
	
	@Override
	public void recite() {
		for(int i=0;i<3;i++){
			System.out.println(context);
		}
	}
	
	public poemImpl(){
		
	}

}
(3)一位会朗诵诗词的杂技师

public class PoeticJuggler extends Juggler{
	
	private Poem poem;
	
	public PoeticJuggler(Poem poem){
		super();
		this.poem=poem;
	}
    
	public PoeticJuggler(int beanBags,Poem pome){
		super(beanBags);
		this.poem=pome;
	}
	
	@Override
	public void perform() throws PerformanceExcetion {
		super.perform();
		System.out.println("并且现在开始背诗词");
		poem.recite();
	}
}
接下来我们要在xml文件声明我们所写的东东,首先要我们要声明吟诵的诗词,然后在通过声明这位会吟诵诗词的杂技师,通过传入参数的方法,进而实现:
 <bean id="poeticJuggler" class="com.entity.PoeticJuggler">
	   		<constructor-arg value="20"></constructor-arg>
	   		<constructor-arg ref="poemImple"></constructor-arg>
</bean>
这了我们通过value传入对应的值,通过ref属性将Bean引用传递给构造器,这样就实现了这位多技能的杂技师。










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值