Spring框架学习笔记1:基本概念和控制反转(IOC)

这是黑马视频的学习笔记

spring的概念

1.spring是一个开源的轻量级框架

2,spring的核心两部分

  aop:面向切面编程,不通过修改源代码来扩展功能

   Ioc:控制反转:有一个类,它有一个非静态方法。要调用这种方法,需要使用对象。一般是new创建对象。但是spring不用这样做,而是由spring核心容器负责创建所有的对象

3.spring是一站式框架

spring在javaee三层结构中,对每一层都提供了解决技术。

web层的技术是SpringMVC。

service层的技术是IOC。

dao层的技术是JdbcTemplate

spring的ioc操作

1.对象的创建由spring框架管理

2.IOc的两种实现方式

 配置文件方式

注解方式

IOC的底层原理

ioc底层原理要使用的技术:

1.xml配置文件

2.dom4j解析xml

3.工厂设计模式

4.反射

IOC的原理图


入门案例

导入jar包====》创建类,里面要有方法======》创建核心配置文件=====》写测试代码进行测试

1.一般导入4个hexinjar包即可,初学者也可以21个jar包全部导入,还要导入日志输出的jar包,common和log4j

3.核心配置文件:名称位置不固定,建议放在src下,application.xml.  引入schema约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"        
    xmlns:mvc="http://www.springframework.org/schema/mvc"     
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    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    
            http://www.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context.xsd    
            http://www.springframework.org/schema/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop.xsd "  
    default-autowire="byName"> 
    <!--使用有参构造方法来注入属性  --> 
<bean id="demo1"  class="cn.itcast.property.PropertyDemo1">
  <constructor-arg  name="username" value="logen"></constructor-arg>

</bean>

<bean id="demo2"  class="cn.itcast.property.Book">
    <property name="bookname" value="九阴真经"></property>
</bean>

</beans>
4.测试
	@Test
	public void testUser() {
	 ApplicationContext context=new ClassPathXmlApplicationContext("bean1.xml");
	   PropertyDemo1 demo1=  (PropertyDemo1) context.getBean("demo1");
	  Book book=(Book) context.getBean("demo2");
	   
	   demo1.test();
	   
	   book.demoBook();
	   

配置文件没有提示问题

spring文件引入的是schema约束,要把约束文件引入到eclipse中。

preferences=====》xml catalog=====》add====》key里面填写约束文件的网址,location找本地地址。key type变成schema  location==》OK

spring的bean管理(xml方式)

1.bean实例化的三种方式

在spring里面使用配置文件创建对象;有三种方式

a . 使用类的无参数构造器

<bean id="demo1"  class="cn.itcast.property.PropertyDemo1">

b.  使用静态工厂方法

c.  使用实例工厂

2.bean 标签的常用属性

id属性,class属性,name属性(功能和id属性一样,只是它的属性值可以包含特殊符号) 。   scope属性:singleton表示单实例。prototype表示多实例

3.属性注入的两种方式

a.  使用set方法注入

类的代码要有set方法

 private String bookname;

public void setBookname(String bookname) {
	this.bookname = bookname;
}

配置文件的代码

<bean id="demo2"  class="cn.itcast.property.Book">
    <property name="bookname" value="九阴真经"></property>
</bean>

b.  使用有参数的构造方法注入、

类的代码要有有参构造器

public PropertyDemo1(String username) {
		this.username = username;
	}

配置文件:

 <!--使用有参构造方法来注入属性  --> 
<bean id="demo1"  class="cn.itcast.property.PropertyDemo1">
  <constructor-arg  name="username" value="logen"></constructor-arg>
</bean> 

4.注入对象类型的属性

创建service类和dao类

package cn.itcast.ioc;

public class UserDao {

	public void add() {
		// TODO Auto-generated method stub
		System.out.println("dao..........");
	}

}

在service类里面把dao类型作为属性,生成变量的set方法

package cn.itcast.ioc;

import org.springframework.messaging.simp.user.UserDestinationMessageHandler;

public class UserService {
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public void add() {
		System.out.println("service...........");
		userDao.add();
	}
}

在配置文件中配置

<!-- 注入对象类型的属性 -->
<bean id="userDao"  class="cn.itcast.ioc.UserDao">
</bean>
<bean id="userService"  class="cn.itcast.ioc.UserService">
<!-- 注入dao对象 -->
    <property name="userDao" ref="userDao"></property>
</bean>

5.p名称空间注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"        
    xmlns:mvc="http://www.springframework.org/schema/mvc"     
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
    xmlns:p="http://www.springframework.org/schema/p"           
    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    
            http://www.springframework.org/schema/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop.xsd "  
    default-autowire="byName"> 

<!--p名称空间注入  -->
<bean id="person"  class="cn.itcast.property.Person"  p:pname="lucy">
</bean>
</beans>

6.注入复杂类型属性

数组

list集合

map集合

properties类型

类的代码:

package cn.itcast.property;
import java.util.List;
import java.util.Map;
import java.util.Properties;
public class Person {
  private String pname;

public void setPname(String pname) {
	this.pname = pname;
}

private String[] arrs;
private List<String> list;
private Map<String, String> map;
private Properties properties;


  public void setArrs(String[] arrs) {
	this.arrs = arrs;
}

public void setList(List<String> list) {
	this.list = list;
}

public void setMap(Map<String, String> map) {
	this.map = map;
}


public void setProperties(Properties properties) {
	this.properties = properties;
}

public void test1(){
	  System.out.println("person........"+pname);
	  System.out.println("list........"+list);
	  System.out.println("map........"+map);
	  System.out.println("properties........"+properties);
  }
}

配置文件注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"        
    xmlns:mvc="http://www.springframework.org/schema/mvc"     
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:aop="http://www.springframework.org/schema/aop"  
    xmlns:context="http://www.springframework.org/schema/context"  
    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    
            http://www.springframework.org/schema/context     
            http://www.springframework.org/schema/context/spring-context.xsd    
            http://www.springframework.org/schema/mvc    
            http://www.springframework.org/schema/mvc/spring-mvc.xsd  
            http://www.springframework.org/schema/tx   
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop  
            http://www.springframework.org/schema/aop/spring-aop.xsd "  
    default-autowire="byName"> 
<!--注入复杂类型的属性值  -->
  <bean id="person" class="cn.itcast.property.Person">
    <property name="arrs">
    <list> <value>小王</value>
     <value>小马</value>
      <value>小松</value>
    </list>
    </property>
    
    <property name="list">
    <list>
     <value>小王</value>
     <value>小马</value>
      <value>小松</value>
    </list>
    </property>
    
    <property name="map" >
    <map>
    <entry key="aa" value="lucy"></entry>
    <entry key="bb" value="mary"></entry>
    <entry key="cc" value="tom"></entry>   
    </map>
    </property>
    
    <property name="properties">
    <props>
    <prop key="dri">root</prop>
    <prop key="username">toot</prop>
    <prop key="password">root</prop>
    </props>   
    </property>
  </bean>

</beans>

IOc和Di的区别

IOc:控制反转

DI:依赖注入,现有IOC,才能有DI

spring整合web项目原理

加载spring核心配置文件,加载这个配置文件是,要使用new,但是这样会降低效率。

解决方法:把加载配置文件、创建对象的过程,在服务器启动时同时完成。

解决过程(原理):已经被封装到spring框架里面

1.要使用ServletContext对象

2.要用到监听器技术:

在服务器启动时,会为每个项目创建一个而且只有一个ServletContext对象。在这个对象创建的时候,使用监听器得到这个对象创建的时间,这样就能同时加载配置文件、创建对象。而创建出来的对象会被放到ServletContext域对象里面(使用setAttribute).当需要获取域对象的时候,使用getAttribute方法


猜你喜欢

转载自blog.csdn.net/shanshuisheng/article/details/80989453