Spring IOC DI AOP 注解配置方式

spring的配置
        1.spring2.5前==xml
        2.spring2.5后==xml+annotation
        3.spring3.0后==JavaConfig+annotation配置类

    spring2.5后=xml+annotation


        目的:

         为了优化一下代码:
            <bean id="" class="" init-method="" destroy-method="" scope="" autowire="">
                <property></property>
                <constructor-arg></constructor-arg>
            </bean>

         注解:

            1.注入类


                替换:<bean id="" class=""></bean>
                位置:类
                语法:@Component(value="注入容器中的id,如果省略id为类名且首字母小写,value属性名称可以省略")
                        @Component
                        eg:Class User{}
                            <bean id="user" class="com.apesource.包.User"></bean>
                                                ||等价于||
                            @Component
                            Class User{}

必须在配置文件中扫描注解:

                <context:component-scan base-package=""></context:component-scan>
                含义:扫描所有被@Component注解所修饰的类,注入容器

                @Repository=====>注入数据访问层
                @Service========>注入业务层
                @Controller=====>注入控制层
                以上三个注解与@Component功能语法一致。都是向容器中注入对象,只是分别代表的意义不同

//Service层
@Service
public class AccountServiceImpl implements IAccountService{
@Autowired
    IAccountDao dao;
}
//dao层
@Repository
public class AccountDaoImpl implements IAccountDao {
    @Autowired
    QueryRunner queryRunner;
}
//Controller
@Controller
public class AccountControllerImpl implements IAccountController {
    @Autowired
    IAccountService service;
}
//普通类的注入
@Component
public class Account {
}

 

            2.注入基本数据,javaBean

                @Value
                    含义:注入基本数据
                    替换:<property></property>
                    修饰:成员变量或对应的set方法
                    语法:@Value("数据内容")
                         @Value("${动态获取}")
                加载指定资源文件:<context:property-placeholder                                                                            ocation="classpath:jdbc.properties"></context:property-placeholder>

                @Autowired
                    替换:DI实现javaBean注入
                    语法:@Autowired(required = "true-默认、false、是否必须进行装配")
                    修饰:成员变量或对应的构造方法
                    含义:按照通过构造方法进行“类型装配”,构造方法可以省略
                    注意:
                        1.默认是按照类型装配且同构造方法
                        2.若容器中有一个类型可以与之匹配则装配成功,若没有一个类型可以匹配则报错
                            NoSuchBeanDefinitionException
                        3.若容器中有多个类型可以与之匹配,则自动切换为按照名称装配,若名称没有对应,则报错
                            NoUniqueBeanDefinitionException

public class Book implements InitializingBean, DisposableBean {
     @Value("com.mysql.cj.jdbc.Driver")
    private String url;
    private String name;
    @Value("${url}")
    private String url;
    @Value("${driver}")
    private String driver;
    @Value("${user}")
    private String user;
    @Value("${pwd}")
    @Autowired
    QueryRunner queryRunner;
}

            3.其他注解


                    @Primary
                        含义:首选项,当类型冲突的情况下,此注解修饰的类被列为首选(备胎扶正)
                        修饰:类
                        注意:不能单独使用,必须与@Component....联合使用
                    @Qualifier(value="名称")
                        含义:按照名称装配
                        修饰:成员变量
                        注意:不能单独使用,必须与@Autowired联合使用
                    @Resource(name="名称")
                        含义:按照名称装配
                        修饰:成员变量
                        注意:单独使用
                    @Scope
                        含义:配置类的作用域
                        修饰:类
                        注意:不能单独使用,必须与@Component....联合使用
                           

@Scope("prototype")
@Scope("singleton")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)

                    @PostConstruct:初始化,修饰方法 替换:init-method
                    @PreDestroy:销毁,修饰方法 替换:destory-method

spring中的新注解

    @Configuration

        作用:指定当前类是一个配置类,替代applicationContext.xml文件

        细节:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写。

    @ComponentScan

        作用:用于通过注解指定spring在创建容器时要扫描的包

        替换:<context:component-scan base-package="com.apesource"></context:component-scan>

    @PropertySource

        作用:用于指定properties文件的位置

        属性:value:指定文件的名称和路径。

        替换:<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    @Bean

        作用:用于把当前方法的返回值作为bean对象存入spring的容器中

        属性:name:用于指定bean的id。当不写时,默认值是当前方法的名称

    @Import

        作用:用于导入其他的配置类

        属性:value:用于指定其他配置类的字节码。

        例子:@Import(SystemSpringConfig.class)

@Configuration
@ComponentScan(basePackages = "com.apesource")
@PropertySource(value="JDBC.properties")
@Import(SystemSpringConfig.class)
public class ApplactionConfig {
    @Value("${url}")
    String url;
    @Value("${driver}")
    String driver;
    @Value("${user}")
    String user;
    @Value("${pwd}")
    String pwd;
    @Bean
    public QueryRunner query(DataSource ds) {
        return new QueryRunner(ds);
    }

}

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值