spring1 配置方式注入

第一步:jar包。maven的pom文件

      

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.0.2.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.9</version>
    </dependency>

大概这么多。下面是基本的代码

实体类

package com.demo.student;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

public class Student {
   
	
	private String name;
    
	private List<String> list;
	
	private Map<String,Object> map;
	private Teacher teacher;
	
	
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<String> getList() {
		return list;
	}
	public void setList(List<String> list) {
		this.list = list;
	}
	public Map<String, Object> getMap() {
		return map;
	}
	public void setMap(Map<String, Object> map) {
		this.map = map;
	}
	public Teacher getTeacher() {
		return teacher;
	}
	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}
	@Override
	public String toString() {
		return "Student [name=" + name + ", list=" + list + ", map=" + map
				+ ", teacher=" + teacher + "]";
	}
	
	
	
   

	

	
	
	
}


package com.demo.student;

import org.springframework.stereotype.Repository;




public class Teacher {
    
	public void stu(){
		System.out.println("Teacher");
	}
}



<?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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        " 
        xmlns:context="http://www.springframework.org/schema/context"    
        >
        
       
        
        <!-- 扫描包 -->
        <context:component-scan base-package="com.demo.listaotowired"/>
        <bean id="teacher" class="com.demo.student.Teacher"></bean>
         <bean id="student" class="com.demo.student.Student">
            <property name="name" value ="hello"></property>
            <property name="teacher" ref="teacher"></property>
            <property name="list">
               <list>
                   <value>1</value>
                   <value>2</value>
               </list>
            </property>
            <!-- map注入-->
            <property name="map">
               <map>
                  <entry key="first" value="first"></entry>
               </map>
            </property>
         </bean>
          
        
        
        
       </beans>

Test代码

package springDemo;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.demo.student.Student;


@RunWith(value=SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class TestAutoired {
   
	@Autowired
	private Student student;
	
	@Test
	public void test(){
		System.out.println(student);
	}
}
输入内容
Student [name=hello, list=[1, 2], map={first=first}, teacher=com.demo.student.Teacher@1d387200]



参考地址https://www.cnblogs.com/sunniest/p/4543271.html    ( Sunnier) 非常感谢



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值