IOC注解

1.@service 服务(注入dao)
2.
@component (把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class=""/>)
3.@controller 控制器(注入服务)
4.
@repository dao(实现dao访问)
5.@Scope:用于实现单例,多例,request,session等的配置
@Resource 注解被用来激活一个命名资源(named resource)的依赖注入,在JavaEE应用程序中,该注解被典型地转换为绑定于JNDI context中的一个对象。
Spring确实支持使用@Resource通过JNDI lookup来解析对象,默认地,拥有与@Resource注解所提供名字相匹配的“bean name(bean名字)”的Spring管理对象会被注入
@Resource(name="dataSource")

@Autowired---自动装配

  此注解实现的自动装配是按照类实现注入,如果出现同名的类,那么此注解方式就不能识别是哪个类实现注入,此时会出现异常;如果为了避免出现异常,可以采用@Qualifier(value="es")(熟悉)实现辅助,

      即Qualifier可以指明对象id;

@Component,@Service,@Controller,@Repository注解的类,并把这些类纳入进spring容器中管理。 
下面写这个是引入component的扫描组件 
<context:component-scan base-package=”com.spring”>    

其中base-package为需要扫描的包(含所有子包)

实例:

user实体类:
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;
    }
}
View Code

 userDao接口:

package com.spring.ioc.mapper;

import com.spring.ioc.entity.UserInfo;

/**
 * Dao层接口
 */
public interface IUserInfoMapper {
    public int addUser(UserInfo info);
}
View Code
userDaoImpl实现类:
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;
    }
}
View Code

    userService业务类接口:

package com.spring.ioc.service;

import com.spring.ioc.entity.UserInfo;

public interface IUserInfoService {
    public int addUser(UserInfo info);
}
View Code

   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);
        }

    }
View Code

   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>
View Code

测试:

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());
    }
}
View Code

结果:

原文地址:https://www.cnblogs.com/mayuan01/p/11776296.html