Spring 下载

进入网站https://repo.spring.io/webapp/#/home

点击左侧第二个图标artifact

然后搜索libs-release-local,再里面找到org/springframework/spring,右键点击spring,选择Native Browser,在列表里找到自己要的版本,点击下载。

spring-5.2.5.RELEASE-dist.zip 

 

spring依赖与commons-logging。

进入网站http://commons.apache.org/

选择The Commons Proper ,在下面列表里找到Logging并点击进入,找到自己需要的版本,http://commons.apache.org/proper/commons-logging/download_logging.cgi

 

Spring 文档

进入https://spring.io/,选择Projects下面的Spring Framework,然后点击Learn,找到对应的版本,点击进入https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/

 

使用示例

使用Eclipse新建java工程,并导入Spring包和commons-logging包。

1、新建包,如com.don.bean,并新建类

package com.don.bean;

public class Hello {

	private String name;
	
	public void setName(String name) 
	{
		this.name  = name;
	}
	
	public void show() 
	{
		System.out.println("Hello" + this.name);
	}
}

 

2、在src目录下新建xml文件,如beans.xml,参考连接里面的beans的xml

https://docs.spring.io/spring/docs/5.2.5.RELEASE/spring-framework-reference/core.html#spring-core

 

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="hello" name="hello" class="com.don.bean.Hello">  
        <!-- collaborators and configuration for this bean go here -->
        <property name="name" value="张三" />
    </bean>


</beans>

3、新建测试包,如com.don.test并新建测试类.

package com.don.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.don.bean.Hello;

public class Test {
	
	public static void main(String[] args) 
	{
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
		Hello hello = (Hello)context.getBean("hello");
		hello.show();
	}
}

4、点击运行查看测试结果

 

beans配置文件有2种装配: 设值装配和构造注入;

设值装配

Bean类必须有一个无参构造方法,Bean类必须为属性提供setter方法。

在配置文件中,使用<property name="" value="" />为每个属性注入值,多个属性对应多个property。

还可支持以下类型

Array, List, Set, Map, Properties

<property name="adminEmails">
        <props>
            <prop key="administrator">administrator@example.org</prop>
            <prop key="support">support@example.org</prop>
            <prop key="development">development@example.org</prop>
        </props>
    </property>
    <!-- results in a setSomeList(java.util.List) call -->
    <property name="someList">
        <list>
            <value>a list element followed by a reference</value>
            <ref bean="myDataSource" />
        </list>
    </property>
    <!-- results in a setSomeMap(java.util.Map) call -->
    <property name="someMap">
        <map>
            <entry key="an entry" value="just some string"/>
            <entry key ="a ref" value-ref="myDataSource"/>
        </map>
    </property>
    <!-- results in a setSomeSet(java.util.Set) call -->
    <property name="someSet">
        <set>
            <value>just some string</value>
            <ref bean="myDataSource" />
        </set>
    </property>

Null注入

<property name="email">
        <null/>
    </property>

构造注入

Bean类必须提供有参构造方法。

在配置文件中, 使用<construct-org>为参数注入值,多个参数对应多个<construct-org>

有3种方式:

<constructor-org index = "0" value ="" /> index表示第几个参数,从0开始。

<constructor-org name= "" value ="" /> name表示参数名称

<constructor-org type = "java.lang.String" value ="" /> type表示参数类型,只使用于构造方法中参数类型不同。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值