标签:
http://repo.spring.io/webapp/search/artifact/?0&q=spring-framework
解压:(Spring目录结构:)
* docs:API和开发规范.
* libs:jar包和源码.
* schema:约束.
核心jar包四个:bean/Core/Context/Expression Language
两个开发包 : log日志包/log4j包
com.itheima.spring.demo1
* UserService
* UserServiceImpl
命名方式:applicationContext.xml
约束文件查找路径:
E:\各种jar包\Spring\spring-framework-3.2.0.RELEASE-dist\spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html----->xsd-config.html---->查找context scheam
注解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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!-- bean definitions here --><!-- 开启spring组件扫描注解 --><context:component-scan base-package="com.mickeymouse.ioc"></context:component-scan></beans>
package com.mickeymouse.ioc;import org.springframework.stereotype.Component; //注解生成一个对象,类似于配置文件中的 : // <bean id="studentService" class="com.mickeymouse.ioc.StudentServiceImpl"></bean>@Component("studentService")public class StudentServiceImpl implements StudentService{public void addStudent(){System.out.println("添加学生成功");}}
/** * spring管理Bean之注解方式 */@Testpublic void test9(){//获取配置文件String path = "applicationContext.xml";//加载配置文件 AbstractApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);StudentService studentService = (StudentService) applicationContext.getBean("studentService"); studentService.addStudent();}
Spring中@Componet的三个衍生注解:(功能目前来讲是一致的)
1. @Conntroller :表现层
2. @Service :业务层
3. @Repository :持久层
这三个注解目的:为了让标注类更加的清晰,Spring在后续的版本会对其加强
@value :用于注入普通类型的属性值
@Antowired :自动装配
默认是按类型进行装配
按名称注入:
@Qualifier:强行使用名称注入(和@Antowired公用)
@Resource 相当于 :用来注入对象引用
业务层:
Dao层
测试类:
public class TestContext {/** * 传统方式业务层调用持久层方法 */@Testpublic void test1(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");ProductsService productsService = applicationContext.getBean("productsService",ProductsService.class); productsService.addProducts();}}
结果:
@Scope( "singleton " )
* singleton:--->单例的
* prototype:-->多例的
Bean的生命周期注解:
@PostConstruct :相当于init-method
@PreDestroy :相当于destroy-method
可任意写两个方法执行初始和销毁
实际开发中还有一种XML和注解整合开发:
* Bean有XML配置.但是使用的属性使用注解注入.
注解整合开发需要用到的是:<context:annotation-config/>
标签:
原文地址:http://my.oschina.net/mickeymouse/blog/518175