IOC注解
@Resource(name="dataSource")
@Autowired---自动装配
此注解实现的自动装配是按照类实现注入,如果出现同名的类,那么此注解方式就不能识别是哪个类实现注入,此时会出现异常;如果为了避免出现异常,可以采用@Qualifier(value="es")(熟悉)实现辅助,
即Qualifier可以指明对象id;
@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。
下面写这个是引入component的扫描组件
<context:component-scan base-package=”com.spring”>
其中base-package为需要扫描的包(含所有子包)
实例:
package com.spring.ioc.entity; public class UserInfo { private Integer stu_id; private String stu_name; public Integer getStu_id() { return stu_id; } public void setStu_id(Integer stu_id) { this.stu_id = stu_id; } public String getStu_name() { return stu_name; } public void setStu_name(String stu_name) { this.stu_name = stu_name; } }
userDao接口:
package com.spring.ioc.mapper; import com.spring.ioc.entity.UserInfo; /** * Dao层接口 */ public interface IUserInfoMapper { public int addUser(UserInfo info); }
package com.spring.ioc.mapper; import com.spring.ioc.entity.UserInfo; import org.springframework.stereotype.Repository; @Repository("iUserInfoMapperImpl") public class IUserInfoMapperImpl implements IUserInfoMapper { @Override public int addUser(UserInfo info) { System.out.println("添加成功!--my"); return 1; } }
userService业务类接口:
package com.spring.ioc.service; import com.spring.ioc.entity.UserInfo; public interface IUserInfoService { public int addUser(UserInfo info); }
userServiceImpl业务实现类:
package com.spring.ioc.service; import com.spring.ioc.entity.UserInfo; import com.spring.ioc.mapper.IUserInfoMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * @Service代表Service实现类的 标识,要让Spring管理该Service <bean id="IUserInfoServiceImpl" class="IUserInfoServiceImppl"></bean> */ @Service("iUserInfoServiceImpl") public class IUserInfoServiceImpl implements IUserInfoService { //植入Dao层对象 //@Resource 默认是根据ByName的方式,但是一旦名字为空,就根据ByType @Autowired private IUserInfoMapper iUserInfoMapper; @Override public int addUser(UserInfo info) { return iUserInfoMapper.addUser(info); } }
applicationContext.xml大配置文件:
<?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:aop="http://www.springframework.org/schema/aop" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--扫描注解:包扫描器--> <context:component-scan base-package="com.spring"></context:component-scan> <!--开启AOP注解支持--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
测试:
package com.spring; import com.spring.ioc.entity.UserInfo; import com.spring.ioc.service.IUserInfoService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class IocTest { public static void main(String[] args) { ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); IUserInfoService bean=(IUserInfoService)context.getBean("iUserInfoServiceImpl"); bean.addUser(new UserInfo()); } }
结果: