首页 > 学院 > 开发设计 > 正文

spring依赖注入的三种方式

2019-11-11 01:58:32
字体:
来源:转载
供稿:网友

1.接口注入

2.getter,setter注入

3.构造器注入

1.接口注入

2.setter注入

     <bean id="book" class="com.PRoperty.Book">		<property name="bookName" value="java核心技术"></property>     </bean> 

package com.property;public class Book {	private String bookName;		public void setbookName(String bookName){		this.bookName = bookName;	}		public void demoBook(){		System.out.println("..........."+this.bookName);	}}3.构造器注入

package com.property;public class Property {	private String name;	public Property(String name){		this.name = name;	}	public void test(){		System.out.println("............."+name);	}	}
     <bean id="property" class="com.chen.property.Property">         <constructor-arg name="name" value="java">               </constructor-arg>     </bean>或者是

     <bean id="property" class="com.property.Property">         <constructor-arg index="0" value="java" >               </constructor-arg>              </bean>

接口注入

package com.hanson.ssm.service;import java.util.List;import java.util.Map;import com.hanson.ssm.pojo.Clothes;public interface ClothesService {	public  List<Clothes> findClothesList();	}@Servicepublic class ClothesServiceImpl implements ClothesService{	@Override	public List<Clothes> findClothesList() {		List<Clothes> clothesList = clothesCustomMappers.findClothesList();		return clothesList;	}}	@Autowired	private ClothesService clothesService;

package com.hanson.ssm.service;import java.util.List;import java.util.Map;import com.hanson.ssm.pojo.Clothes;public interface ClothesService {	public  List<Clothes> findClothesList();	}@Servicepublic class ClothesServiceImpl implements ClothesService{	@Override	public List<Clothes> findClothesList() {		List<Clothes> clothesList = clothesCustomMappers.findClothesList();		return clothesList;	}}	@Autowired	private ClothesService clothesService;
	@Autowired	private ClothesService clothesService;


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表