一.属性与成员变量的区别:

属性:对外暴露的,getxxx/setxxx称为属性;

成员变量:private String name称为成员变量或字段

二.applicationContext.xml的书写

<!--约束-->

<beans xmlns:context="http://www.springframework.org/schema/context"

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-4.2.xsd
                     http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">

</beans>

<!--在spring的配置文件中开启spring对注解ioc的支持,指定spring初始化时要扫描的包—>

<context:component-scan base-package="com.itheima"></context:component-scan>

<!--读取数据库配置文件—>

<context:property-placeholder location="classpath:db.properties"/>

<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
     <property name="driverClass" value="${jdbc.driverClass}"></property>
     <property name="jdbcUrl" value="${jdbc.url}"></property>
     <property name="user" value="${jdbc.user}"></property>
     <property name="password" value="${jdbc.password}"></property>

</bean>

三.注解释义

@component:把资源让spring来管理。相当于在xml中配置一个bean。如果不指定value属性,默认bean的id是当前类的类名。首字母小写。      

@Controller:一般用于表现层的注解。

@Service:一般用于业务层的注解

@Repository :一般用于持久层的注解

细节:如果注解中有且只有一个属性要赋值时,且名称是value,value在赋值是可以不写。

@Autowired:

作用:自动按照类型注入。

当使用注解注入属性时,set方法可以省略。它只能注入其他bean类型。当有多个类型匹配时,使用要注入的对象变量名称作为bean的id,在spring容器查找,找到了也可以注入成功。找不到就报错。

@Qualifier:在自动按照类型注入的基础之上,再按照Bean的id注入。它在给字段注入时不能独立使用,必须和@Autowire一起使用;但是给方法参数注入时,可以独立使用。

@Resource:直接按照Bean的id注入。它也只能注入其他bean类型。

@Value:注入基本数据类型和String类型数据的

@Scope:指定bean的作用范围。value:指定范围的值。取值:singleton  prototype request session globalsession

@PostConstruct:用于指定初始化方法。

@PreDestroy:用于指定销毁方法

@Configuration:用于指定当前类是一个配置类,会从该类上加载注解。读取该类上@ ComponentScan注解初始化spring容器。

@ComponentScan:用于指定spring在初始化容器时要扫描的包,(xml中需要basePackages属性,用于指定要扫描的包)。和该注解中的value属性作用一样。

@PropertySource:用于加载.properties文件中的配置,value[]:用于指定properties文件位置。如果是在类路径下,需要写上classpath:

@Import:用于导入其他配置类,value[]:用于指定其他配置类的字节码。

@Bean:该注解只能写在方法上,表明使用此方法创建一个对象,并且交给spring管理。name:给当前@Bean注解方法创建的对象指定一个名称(即bean的id)。

@RunWith注解替换原有运行器;@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration指定spring配置文件的位置;@ContextConfiguration(locations={"classpath:bean.xml"})

spring笔记2-注解的更多相关文章

  1. Spring笔记02_注解_IOC

    目录 Spring笔记02 1. Spring整合连接池 1.1 Spring整合C3P0 1.2 Spring整合DBCP 1.3 最终版 2. 基于注解的IOC配置 2.1 导包 2.2 配置文件 ...

  2. Spring笔记04_AOP注解开发_模板_事务

    目录 1. Spring基于AspectJ的注解的AOP开发 1. 1 SpringAOP的注解入门 1.2 Spring的AOP的注解通知类型 1.2.1 @Before:前置通知 1.2.2 @A ...

  3. Spring笔记13--SSH--全注解开发

    SSH全注解开发: (1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace.@ParentPackage.@Action...) package com.tongji. ...

  4. spring笔记--通过注解(annotation)配置Bean

    Spring能够在classpath下自动扫描,侦测和实例化具有特定注解的组件,这在Spring中成为组件扫描(Component scanning). 特定组件的注解包括: @Component:基 ...

  5. spring笔记-@Primary注解

    1.问题 当一个接口有2个不同实现时,使用@Autowired注解时会报org.springframework.beans.factory.NoUniqueBeanDefinitionExceptio ...

  6. Spring学习笔记--使用注解装配

    使用@Autowired注解 从Spring2.5开始,最有趣的一种装配Spring Bean的方式是使用注解自动装配Bean的属性.Spring默认禁用注解装配,最简单的启用方式是使用Spring的 ...

  7. Spring笔记(5) - 声明式事务@EnableTransactionManagement注解源码分析

    一.背景 前面详解了实现Spring事务的两种方式的不同实现:编程式事务和声明式事务,对于配置都使用到了xml配置,今天介绍Spring事务的注解开发,例如下面例子: 配置类:注册数据源.JDBC模板 ...

  8. springmvc学习笔记(常用注解)

    springmvc学习笔记(常用注解) 1. @Controller @Controller注解用于表示一个类的实例是页面控制器(后面都将称为控制器). 使用@Controller注解定义的控制器有如 ...

  9. 学习笔记_J2EE_SpringMVC_03_注解配置_@RequestMapping用法

    @RequestMappingde的用法 摘要: 主要介绍注解@RequestMapping的用法 一.@RequestMapping 简介 在Spring MVC 中使用 @RequestMappi ...

  10. Spring笔记01_下载_概述_监听器

    目录 Spring笔记01 1.Spring介绍 1.1 Spring概述 1.2 Spring好处 1.3 Spring结构体系 1.4 在项目中的架构 1.5 程序的耦合和解耦 2. Spring ...

随机推荐

  1. ssh 配置无密码登录

    下框中在管理机上运行: [root@master ~]# ssh-keygen -t rsa #它在/root/.ssh下生成id_rsa和id_rsa.pub两个文件 [root@master ~] ...

  2. win7 宽带连接 711错误

    新装Win7系统,建立宽带连接后提示711错误,网上转一圈,方法不少,对症下药,不是很管用 以下是我总结的方法 win7 旗舰版 administration 用户 设置以下几个服务为手动或自动,并启 ...

  3. complex 类

    //定义一个复数类Complex,使得下面的代码能够工作. //Complex c1(3,5)//用复数3+5i初始化c1 //complex c2=4.5//用实数4.5初始化c1 //comple ...

  4. Mysql-proxy代理内网数据库

    Mysql-proxy 参考:https://segmentfault.com/q/1010000000394160 情景分析:首先您需要正在使用UCloud云主机(uhoust)以及云数据库(udb ...

  5. HDU-Fibonacci Again(打表找规律)

    There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). ...

  6. 江西财经大学第一届程序设计竞赛 C

    链接:https://www.nowcoder.com/acm/contest/115/C来源:牛客网 题目描述 决赛圈还剩下两个人,“伏地魔”XDD和跑毒进圈的FZL,XDD拿着狙击枪AWM瞄准并准 ...

  7. cf Permute Digits(dfs)

    C. Permute Digits You are given two positive integer numbers a and b. Permute (change order) of the ...

  8. 关于Yii2中的MVC中的视图总结(持续更新中)

    一.首先在控制器中,将处理好的数据发送给前台: $this->layout = 'base'; 这里填写视图的模板文件(可以不写这行代码,如果不写,默认为views/layouts/main.p ...

  9. ssis error at other ssis.pipeline "ole db destination" failed validation and returned validation status

    我在修改一个ssis的包,发现这个destination的表被改过了.所以就重建了表.就导致了这个错误. 打开包重新检查下表结构的匹配就好了

  10. 德国生活tips

    提要: 在德国生活也近7个月的时间了,简单给准备来德国留学,生活或者是旅游的人写一些小tips.想到什么就写什么咯. (1)德国交通篇 在德国,交通是第一要点,一般大家都会看到城市里有Straßenb ...