Spring的入门及案例----Ioc

一:Spring的核心

Spring的核心是控制反转(IoC)面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架

  • IoC:控制反转。
    对于Spring来说,Spring创建对象的过程,不是在代码里面实现,而是交给Spring来进行配置实现的。(Bean.xml)
  • AOP:面向切面编程。 

二:Spring的特点

l  方便解耦,简化开发  (高内聚低耦合)

•    Spring就是一个大工厂(容器),可以将所有对象创建和依赖关系维护,交给Spring管理

•    spring工厂是用于生成bean

l  AOP编程的支持

•    Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能

l  声明式事务的支持

•    只需要通过配置就可以完成对事务的管理,而无需手动编程

l  方便程序的测试

•    Spring对Junit4支持,可以通过注解方便的测试Spring程序

l  方便集成各种优秀框架

•    Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持

l  降低JavaEE API的使用难度

•    Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低

三:入门案例Ioc

简介:Ioc就是让Spring去创建类的实例,而并非去类中去创建。

<bean id="userServiceId" class="com.hao.a_ioc.UserServiceImpl"></bean>

1.导入jar包:

                  4 + 1  : 4个核心(beans、core、context、expression) + 1个依赖(commons-loggins...jar)

2.提供接口和实现类(UserService)

public interface UserService {
	
	public void addUser();

}
public class UserServiceImpl implements UserService {

	@Override
	public void addUser() {
		System.out.println("ico add user");
	}

}

3.配置文件

l  位置:任意,开发中一般在classpath下(src)

l  名称:任意,开发中常用applicationContext.xml

l  内容:添加schema约束

       约束文件位置:spring-framework-3.2.0.RELEASE\docs\spring-framework-reference\html\xsd-config.html


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 配置service 
		<bean> 配置需要创建的对象
			id :用于之后从spring容器获得实例时使用的
			class :需要创建实例的全限定类名
	-->
	<bean id="userServiceId" class="com.hao.a_ioc.UserServiceImpl"></bean>
</beans>

4.测试

    @Test
	public void demo02(){
		//从spring容器获得
		//1 获得容器
		String xmlPath = "com/hao/a_ioc/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		//2获得内容 --不需要自己new,都是从spring容器获得
		UserService userService = (UserService) applicationContext.getBean("userServiceId");
		userService.addUser();
		
	}


四:入门案例DI

简介:DI(Dependency Injection )依赖注入:在创建实例的过程中,向类里面的属性中设置值。

模拟spring执行过程

       创建service实例:BookService bookService = newBookServiceImpl()      -->IoC  <bean>

       创建dao实例:BookDao bookDao = new BookDaoImple()                          -->IoC

       将dao设置给service:bookService.setBookDao(bookDao);                         -->DI   <property>

步骤:

    l       创建BookDao接口和实现类

    l       创建BookService接口和实现类

    l       将dao和service配置 xml文件

    l       使用api测试



public interface BookDao {
	
	public void addBook();

}
 
 
 
 
public class BookDaoImpl implements BookDao {

	@Override
	public void addBook() {
		System.out.println("di  add book");
	}

}
public interface BookService {

	public abstract void addBook();

}
 
 
public class BookServiceImpl implements BookService {
	
	// 方式1:之前,接口=实现类
//	private BookDao bookDao = new BookDaoImpl();
	// 方式2:接口 + setter
	private BookDao bookDao;
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}
	
	@Override
	public void addBook(){
		this.bookDao.addBook();
	}

}

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       					   http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- 
	模拟spring执行过程
		创建service实例:BookService bookService = new BookServiceImpl()	IoC  <bean>
		创建dao实例:BookDao bookDao = new BookDaoImpl()			IoC
		将dao设置给service:bookService.setBookDao(bookDao);		DI   <property>
		
		<property> 用于进行属性注入
			name: bean的属性名,通过setter方法获得
				setBookDao ##> BookDao  ##> bookDao
			ref :另一个bean的id值的引用
	 -->

	<!-- 创建service -->
	<bean id="bookServiceId" class="com.itheima.b_di.BookServiceImpl">
		<property name="bookDao" ref="bookDaoId"></property>
	</bean>
	
	<!-- 创建dao实例 -->
	<bean id="bookDaoId" class="com.itheima.b_di.BookDaoImpl"></bean>
	

</beans>
@Test
	public void demo01(){
		//从spring容器获得
		String xmlPath = "com/hao/b_di/beans.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		BookService bookService = (BookService) applicationContext.getBean("bookServiceId");
		
		bookService.addBook();
		
	}

五:装配Bean

Bean的实例化方式:默认构造,静态工厂, 实例工厂

 

l  实例工厂:必须先有工厂实例对象,通过实例对象创建对象。提供所有的方法都是“非静态”的。

工厂:

/**
 * 实例工厂,所有方法非静态
 *
 */
public class MyBeanFactory {
	
	/**
	 * 创建实例
	 * @return
	 */
	public UserService createService(){
		return new UserServiceImpl();
	}

}

Spring配置

<!-- 创建工厂实例 -->
	<bean id="myBeanFactoryId" class="com.itheima.c_inject.c_factory.MyBeanFactory"></bean>
	<!-- 获得userservice 
		* factory-bean 确定工厂实例
		* factory-method 确定普通方法
	-->
	<bean id="userServiceId" factory-bean="myBeanFactoryId" factory-method="createService"></bean>

六:装配Bean基于注解

l  注解:就是一个类,使用@注解名称

l  开发中:使用注解 取代 xml配置文件。

1. @Component取代<bean class="">

       @Component("id")取代 <beanid="" class="">

2.web开发,提供3个@Component注解衍生注解(功能一样)取代<beanclass="">

       @Repository:dao层

       @Service:service层

       @Controller:web层

3.依赖注入    ,给私有字段设置,也可以给setter方法设置

       普通值:@Value("")

       引用值:

              方式1:按照【类型】注入

                     @Autowired

              方式2:按照【名称】注入1

                     @Autowired

                     @Qualifier("名称")

              方式3:按照【名称】注入2

                     @Resource("名称")

4.生命周期

       初始化:@PostConstruct

       销毁:@PreDestroy

5.作用域

       @Scope("prototype")多例

l  注解使用前提,添加命名空间,让spring扫描含有注解类

<!-- 组件扫描,扫描含有注解的类 -->
	<context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan>

<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.xsd
       					   http://www.springframework.org/schema/context 
       					   http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 组件扫描,扫描含有注解的类 -->
	<context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan>
</beans>

猜你喜欢

转载自blog.csdn.net/ZHANGLIHAOOO/article/details/80245358