spring1 配置方式注入
来源:互联网 发布:2017年泰国耽美网络剧 编辑:程序博客网 时间:2024/06/08 07:40
第一步: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;}@Overridepublic 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 { @Autowiredprivate Student student;@Testpublic void test(){System.out.println(student);}}
输入内容
Student [name=hello, list=[1, 2], map={first=first}, teacher=com.demo.student.Teacher@1d387200]
阅读全文
0 0
- spring1 配置方式注入
- Spring1.x Aop配置方式
- 对比Spring1.0 与 2.0 的事务配置方式
- Spring1.0 与 2.0 事务配置方式的对比
- Spring1
- spring1
- spring1
- Spring1
- Spring1
- spring1
- spring1
- Spring1
- Spring1
- spring1
- 依赖注入的方式和注入的配置实例
- 注解方式配置SessionFactory的注入
- Spring-Boot-多种配置注入方式
- Spring三种配置注入方式
- Python多元线性回归-sklearn.linear_model,并对其预测结果评估
- 深入浅出进程关系分析Job Control(一)
- typedef的用法总结
- curl安装与命令使用 win+linux
- java list 转 树形json 递归效率对比
- spring1 配置方式注入
- RocketMQ原理解析-producer 2.如何发送消息
- 使用canvas绘制风景时钟
- 【运维】浅识监控
- 解决跨域问题的8种方案(最新最全)
- Starting Tomcat v7.0 Server at localhost' has encountered a problem
- [Kaggle] Titanic: Machine Learning from Disaster入门版练习笔记
- [Mysql]——事务的4种隔离级别
- 【Leetcode】 1.two sum unordered_map 关联容器