spring怎样注入集合,list,set,map各怎样注入
2个回答
展开全部
package com.spring.test.springSession.service;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class AOPService {
private Map<String ,Object> exampleMap ;
private List<Object> exampleList ;
private Set<Object> exampleSet ;
public Map<String, Object> getExampleMap() {
return exampleMap;
}
public void setExampleMap(Map<String, Object> exampleMap) {
this.exampleMap = exampleMap;
}
public List<Object> getExampleList() {
return exampleList;
}
public void setExampleList(List<Object> exampleList) {
this.exampleList = exampleList;
}
public Set<Object> getExampleSet() {
return exampleSet;
}
public void setExampleSet(Set<Object> exampleSet) {
this.exampleSet = exampleSet;
}
public String AopSercieMethod(){
System.out.println("exampleList :"+exampleList);
System.out.println("exampleMap :"+exampleMap);
System.out.println("exampleSet :"+exampleSet);
return "这是xx的Spring-Session测试";
}
}
以上是等待被注入的类
下面是配置类
package com.spring.test.springSession.config;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.spring.test.springSession.aop.AOPAspect;
import com.spring.test.springSession.service.AOPService;
@Configuration
public class AOPConfig {
@Bean
public AOPService getAopService(){
AOPService aopService = new AOPService();
List<Object> exampleList = new ArrayList<Object>();
exampleList.add("我是一个list注入属性");
Set<Object> exampleSet = new HashSet<Object>();
exampleSet.add("我是一个set注入属性");
Map<String,Object> exampleMap = new HashMap<String,Object>();
exampleMap.put("你好", "我是一个Map注入");
aopService.setExampleList(exampleList);
aopService.setExampleSet(exampleSet);
return aopService;
}
}
然后是配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config />
<bean class="com.spring.test.springSession.config.AOPConfig"/>
</beans>
然后是测试类
package com.spring.test.springSession.test;
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.spring.test.springSession.service.AOPService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:**/spring.xml"})
public class InjectTest {
@Autowired
AOPService aopService ;
@Test
public void test(){
aopService.AopSercieMethod();
}
}
然后结果
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2016-10-26
展开全部
<bean class="java.util.ArrayList" id="mylise"/>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询