spring framework入门(2):spring配置和启动方式

工具:maven、spring framework(4.2.4.RELEASE)

示例代码:https://download.csdn.net/download/u010476739/11419584

spring framwork的核心功能是依赖注入、作为对象工厂

试验条件:

maven

spring framwork 4.2.4.RELEASE

试验目的:

试验spring配置文件的加载方式,以及使用注解类代替配置文件启动容器。

1. 首先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>com.jackletter</groupId>
  <artifactId>hellospring</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>this is name</name>
  <description>this is desc</description>
  
  <properties>
  	<spring.version>4.2.4.RELEASE</spring.version>
  </properties>
  
  <dependencies>
 
        <!-- Spring Core -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
         
        <!-- Spring Context -->
        <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
         
    </dependencies>
</project>

2.准备applicationContext.xml

<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.xsd">
	<bean id="dog" class="springconf.Dog"></bean>
	<bean id="cat" class="springconf.Cat"></bean>
	<bean id="srv" class="springconf.Srv">
		<property name="animate" ref="dog"></property>
	</bean>
</beans>

3.代码结构

4. 代码

Animate.java

package springconf;

public interface Animate {
	public String show();
}

Cat.java

package springconf;

public class Cat implements Animate {
	public String show() {
		return "i'm Cat";
	}
}

Dog.java

package springconf;

public class Dog implements Animate {

	public String show() {
		return "i'm Dog";
	}

}

Srv.java

package springconf;

public class Srv {
	public Animate animate;

	public Animate getAnimate() {
		return animate;
	}

	public void setAnimate(Animate animate) {
		this.animate = animate;
	}

	public String show() {
		return animate.show();
	}
}

AppConfig.java

package springconf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

	@Bean(name="srv")
	public Srv getSrv() {
		Srv srv = new Srv();
		srv.setAnimate(new Cat());
		return srv;
	}
}

App.java

package springconf;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {
	static ApplicationContext context=null;
	public static void main(String[] args) {
		//1. 寻找spring配置文件方式1 这个文件在"springconf\src\main\resources\applicationContext.xml"
		context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
		//2. 寻找spring配置文件方式2 效果等同上面
		//context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
		//3. 寻找spring配置文件方式3 使用绝对路径,一般不使用
		//context = new ClassPathXmlApplicationContext("file:E:/elipsespace/springconf/target/classes/applicationContext.xml");
		
		//4.使用注解类加载
	    context = new AnnotationConfigApplicationContext(AppConfig.class);		
	    
		System.out.println(((Srv) context.getBean("srv")).show());
		
		
	}
}

5.运行

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jackletter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值