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

属性:对外暴露的,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"})

最新文章

  1. 系统级性能分析工具perf的介绍与使用
  2. java25
  3. 遗传算法在JobShop中的应用研究(part4:变异)
  4. HttpClient(4.3.5) - HTTP Request &amp; HTTP Response
  5. IOS 10适配https 包含对于一些http的一些兼容配置
  6. HttpClient3.1 警告: Cookie rejected:
  7. C++ Primer笔记(一):字符串、向量和数组
  8. 继刚接触play framework后,一些心得
  9. 时刻注意QT与Windows系统的不同(惨痛教训)
  10. silverlight依赖属性
  11. 关于 WP上应用调试时报错“指定的通信资源(端口)”已由另一个应用程序使用 问题
  12. xhr.readyState就绪状态
  13. 基本的传染病模型:SI、SIS、SIR及其Python代码实现
  14. Hibernate update 和 merge 、saveOrUpdate的区别
  15. Java calendar获取月份注意事项
  16. centos7 安装 pyspider 出现的一系列问题及解决方案集合
  17. [Swift]LeetCode301. 删除无效的括号 | Remove Invalid Parentheses
  18. java实现哈夫曼编码
  19. 任意模数NTT
  20. nvidia-docker2配置与NVIDIA驱动安装

热门文章

  1. sql开发技巧总结-1
  2. springboot结合swagger生成接口文档
  3. t-sql read xlsx
  4. 记录下自己安装cuda以及cudnn
  5. ueditor chrome bug
  6. js-带操作的赋值表达式
  7. java mybatis学习一
  8. Java中Array与ArrayList的主要区别
  9. 暴力打表之hdu 2089
  10. Java学习笔记day06_自定义类_ArrayList