Spring 中@Autowired的使用。

自动装配分为四种:

1、byName。2、byType。3、constructor。4、autodetect。


一般用到的是自动装配,前面需要加上自动装配的标识。<context:annotation-config/>。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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">
      
<context:annotation-config/>

</beans>

@Autow ire需要添加在相应的daoimpl之前,或者set方法上面。


代码:

package com.dao;


import com.vo.Students;


public interface StudentsDAO {


public boolean saveStudents(Students s);
}



package com.dao.impl;


import com.dao.StudentsDAO;
import com.sun.org.apache.regexp.internal.recompile;
import com.vo.Students;


public class StudentsDAOImpl implements StudentsDAO {


@Override
public boolean saveStudents(Students s) {
// TODO Auto-generated method stub
if (s!=null) {
System.out.println("s.getId() : " + s.getId());
System.out.println("s.getName() : " + s.getName());
System.out.println("s.getAge() : " + s.getAge());

return true;
}else {
return false;
}
// return false;
}

}



package com.service;


import java.util.Map;
import java.util.Set;


import org.springframework.beans.factory.annotation.Autowired;


import com.dao.impl.StudentsDAOImpl;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;
import com.vo.Bar;
import com.vo.Baz;
import com.vo.Students;




public class StudentsService {

// @Autowired
private StudentsDAOImpl sDAO;
private Students stu;
private Map<String, String> map;

private Bar bar;
private Baz baz;

// @Autowired
public void saveStudents() {
if (stu!=null) {
sDAO.saveStudents(stu);
// System.out.println(list.toString());
// System.out.println(set.size());
System.out.println("one : " + map.get("one"));
System.out.println("two : " + map.get("two"));
System.out.println("six : " + map.get("six"));
}else {
System.out.println("error!");
}
}


public StudentsDAOImpl getsDAO() {
return sDAO;
}
@Autowired
public void setsDAO(StudentsDAOImpl sDAO) {
this.sDAO = sDAO;
}


public Students getStu() {
return stu;
}


public void setStu(Students stu) {
this.stu = stu;
}


public Bar getBar() {
return bar;
}


public void setBar(Bar bar) {
this.bar = bar;
}


public Baz getBaz() {
return baz;
}


public void setBaz(Baz baz) {
this.baz = baz;
}


// public List getList() {
// return list;
// }
//
// public void setList(List list) {
// this.list = list;
// }
//
// public Set getSet() {
// return set;
// }
//
// public void setSet(Set set) {
// this.set = set;
// }
//
public Map<String, String> getMap() {
return map;
}


public void setMap(Map<String, String> map) {
this.map = map;
}




}

package com.vo;


public class Bar {

private String id;
private String name;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}



}

package com.vo;


public class Baz {

private String id;
private String name;

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}




}

package com.vo;


import org.springframework.beans.factory.annotation.Autowired;


public class Students { 

private String id;
private String name;
private String age;

public Students() {

}
public Students(String id,String name,String age) {
this.id=id;
this.name=name;
this.age=age;
}

public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}



}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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">
      
<context:annotation-config/>
  <bean name="studentsDAO" class="com.dao.impl.StudentsDAOImpl"></bean>
  <bean name="serviveBean" class="com.service.StudentsService"></bean>
 
  <bean name="studentsService" class="com.service.StudentsService">
  <property name="sDAO" ref="studentsDAO"></property>
  <property name="stu">
  <bean class="com.vo.Students">
  <property name="id" value="001"></property>
  <property name="name" value="002"></property>
  <property name="age" value="003"></property>
  </bean>
  </property>
  <property name="map">
  <map>
  <entry key="one" value="9.99"></entry>
  <entry key="two" value="2.99"></entry>
  <entry key="six" value="3.79"></entry>
  </map>
  </property>
  </bean>
 
  <!--  需要用static描述类
  <bean id="studentsService" class="com.service.StudentsService" factory-method="work">
  <property name="sDAO" ref="studentsDAO"></property>
  <property name="stu" ref="students"></property>
  </bean>
  -->
</beans>



package com.service;


import javax.faces.application.Application;


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


import com.vo.Students;




import junit.framework.Assert;
import junit.framework.TestCase;


public class TestService extends TestCase {


public void testService() {
ApplicationContext ctx =new  ClassPathXmlApplicationContext("beans.xml");
// Students s =(Students) ctx.getBean("students");
// s.setAge("13");
// s.setId("001");
// s.setName("张三风");

StudentsService service = (StudentsService) ctx.getBean("studentsService");
// service.saveStudents(s);
// Assert.assertEquals(true, service.saveStudents());
service.saveStudents();

}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值