Spring框架基础
来源:互联网 发布:啊哈c语言 小木虫 编辑:程序博客网 时间:2024/05/16 04:56
Spring是一个开源的轻量级的应用开发框架,其目的是用于简化企业级应用程序的开发,降低侵入性。
Spring本质:管理软件中的对象,创建对象和维护对象之间的关系。
Spring实现了IOC和AOP功能,可以简化Bean对象创建和Bean对象之间的解耦。
Spring容器有BeanFactory和ApplicationContext两种类型。其中ApplicationContext继承自BeanFactory接口。
例如:String conf = "applicationContext.xml"; ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
主要方法:getBean(beanName)获取Bean对象
Spring容器创建Bean对象的方法有3种:
1、用构造器实例化。例如:<bean id="" class=""/>
2、静态工厂方法实例化。 例如:<bean id="" class="" factory-method=""/>
3、对象工厂方法实例化。 例如:<bean id="" factory-bean="" factory-method=""/>
bean对象的别名:<alias name="" alias=""/>
bean对象的作用域:singleton,prototype,request,session,global Session
bean定义的属性:id,name,class,init-method,destroy-method,lazy-init,autowire
<beans>定义的属性:default-init-method,default-destory-method,default-lazy-init
IOC:Inversion of Control,控制反转(程序中对象的获取方式发生反转,由最初的new方式创建,转变为由第三方框架创建,注入(DI),它降低了对象之间的耦合度)
DI:Dependency Injection,依赖注入。(将一起工作具有关系的对象,通过构造方法参数或方法参数传入建立关联,因此容器的工作就是创建bean时注入这些依赖关系)分为:Setter注入,构造器注入。
<bean id="" class=""> <property name="" value=""/></bean><bean id="" class=""> <constructor-arg index="0" value=""/></bean>
自动装配(outowire):操作bean对象之间的关联关系。
分为5种:no(默认值),byName,byType,contructor,autodetect
参数值注入:
<property name="name"> <value></value></property><property name="name" value=""/><property name="" ref=""/><property name="lists"> <list> <value></value> </list></property><property name="sets"> <set> <value></value> </set></property><property name="score"> <map> <entry key="" vlaue=""/> </map></property><property name="props"> <props> <prop key=""></prop> </props></property><util:list id="langList"> <value></value></util:list><property name="langs" ref="langList"><util:list>,<util:set>,<util:properties>都可以采用这种方式<util:properties id="const" location="classpath:const.properties"><property name="" value="#{msg.name}"/>注入null值<property name="name"> <null/></property>
基于注解的组件扫描:
组件扫描可以代替大量的XML配置的<bean>定义。
<context:component-scan base-package="com.model"/>
常用的注解标记:@Component ,@Named,@Repository,@Service,@Controller
生成的bean的默认为类名首字母小写,当然我们可以指定bean的id。例如:@Component("test")
作用域:@Scope("prototype"),
指定初始化和销毁回调方法:@PostConstruct,@PreDestroy
依赖关系的注入:
1、@Autowired / @Qualifier 构造器和Setter注入都可以使用。
例如:
@Autowired
public Test(@Qualifier("test") Test test) {
this.test = test;
}
注入的对象为单例时,@Quealifier可以省略。
2、@Inject / @Named
3、@Resource 只能处理Setter注入。通常将@Resource注解写在set方法上,如果写在属性上,则只会执行一行代码this.name = name。
4、@Value注解可以注入Spring 表达式的值。例如:@Value("#{const.username}")
- Spring 框架基础结构:
- Spring框架基础概念
- spring框架基础
- Spring框架基础示例
- Spring框架基础
- spring框架基础学习
- Spring MVC 框架基础
- Spring框架基础
- Spring框架学习【AOP基础】
- Spring框架基础篇【一】
- 学习spring框架必备基础
- Spring框架基础之注解
- Spring框架基础梳理-1
- [Spring框架]Spring JDBCTmplate基础入门总结.
- [Spring框架]Spring 事务管理基础入门总结.
- Spring(一)Spring框架基础概念
- spring框架基础与IoC基础
- Spring框架的基础使用(Spring框架的入门)
- 商业研究(16):实体经济之殇,互联网经济之困?
- 最大报销额 HDU 1864
- 机器学习(一)--- 监督学习之回归
- 写给考完SDOI2016R2D1的自己
- 只要你停止进食,疾病就无处生长,只要你停止思考,烦恼就戛然而止
- Spring框架基础
- 大量 Material Design 学习资源
- FileOutputStream和FileInPutputStream流的联合使用
- JS概述
- 关于三年工作的一点点感悟
- cocos-creator 开发路程一实现可移动的游戏选择窗口(二)
- 炸弹人
- gettimeofday()、tzset()与localtime()
- VMware 下 Ubuntu 使用 Share folder 共享 WIN7 文件