一,添加spring框架所需要的jar包(有其它不相关的包,分不清了)

Spring框架基础(一)实例化类——1.bean方式2.注解方式Spring框架基础(一)实例化类——1.bean方式2.注解方式

二,创建spring的配置文件——通常放在src目录下,命名:applicationContext.xml(或者spring-config.xml,自定义也行)

1.xml文件的头部

context:开启注解扫描(使用注解进行实例化/DI_IOC时使用)

aop:面向切面(为第三方【无法变动源码】添加新功能)编程时使用

tx:使用事务功能时添加

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-4.1.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">

</beans>

2.使用bean标签实现实例化,class:类名,  name:实例化后的对象名

<beans ..........>

        <bean ...........>

               .........................

        </bean>

</beans>

        <!-- spring功能1:实例化 -->
        <!-- 1.1自定义类
        java中实例化的方式
        1.导入类后通过类名实例化:Bookinfo bookinfo = new Bookinfo();
        2.不导入类直接写全类名
	com.lanou.fuxi.domain.Bookinfo bookinfo = new com.lanou.fuxi.domain.Bookinfo();
	-->
	<bean name="bookinfo" class="com.lanou.fuxi.domain.Bookinfo"></bean>
	
	<!-- 1.2:实例化第三方类 
		java.util.List students = new java.util.ArrayList
	-->
	<bean name="students" class="java.util.ArrayList"></bean>

测试类代码:

public static void main(String[] args) {
		//com.lanou.fuxi.domain.Bookinfo bookinfo = new com.lanou.fuxi.domain.Bookinfo();
		
		//读取SPRING配置文件,来测试xml配置文件的实例化功能
                //classpath:根路径(src或者webcontent)
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-config_xml.xml");
		
		//调用XML实例化的2个类,通过getBean("bean的name的值")方法获取实例化的类
		Bookinfo bookinfo = (Bookinfo)ctx.getBean("bookinfo");
		System.out.println(bookinfo.getBookid());
		
		List students = (List)ctx.getBean("students");
		System.out.println(students.size());
		
	}

3.使用注解方式实例化

        <!-- 开启SPRING注解实现实例化功能,记得在上面开启 context
	    @Component(通用,分不清就都用这个,反正都是实例化)
            @Service(业务层),@Repository(持久化层)  @Controller(控制层)
            1.实例化后name默认为这个类首字母小写的类名
            2.自定义实例化后的name:@Component("XXXX")
            3.书写位置:在类名的上方写,public class 类名{}的上方
            base-package=“”,需要扫描的类
	 -->		
	<context:component-scan base-package="com.lanou.fuxi.domain"/>

测试类代码:

public static void main(String[] args) {
		// 读取SPRING配置文件,来测试xml配置文件的实例化功能
		ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring-config_annotation.xml");

		// 调用XML实例化的2个类
		Bookinfo bookinfo = (Bookinfo) ctx.getBean("bookinfo");
		System.out.println(bookinfo.getBookid());

		//此处没办法用,因为第三方只能在XML里面<bean>来实例化,因为没有源码没办法在类里面@component实例化
//		List students = (List) ctx.getBean("students");
//		System.out.println(students.size());
	
	}

三,后附jar包与源码

。。。。。。。。。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2022-02-13
  • 2021-04-04
  • 2021-06-29
猜你喜欢
  • 2021-06-17
  • 2022-12-23
  • 2021-10-18
  • 2023-03-20
  • 2021-10-08
  • 2021-06-11
相关资源
相似解决方案