Spring初体验之IOC

Spring初体验之IOC

  • IOC
    • 原理
    • xml方式
      • 基于xml创建对象
      • 基于xml实例化对象(注入属性)
      • 基于xml调用对象
      • 基于xml注入集合属性
      • xml自动装配
      • 基于xml连接数据库
    • 注解方式
      • 基于注解创建对象
      • 基于注解实例化对象(注入属性)
      • 完全注解开发

IOC

原理

spring是一个管家,将对象创建和调用的任务交给它去打理

xml方式

基于xml创建对象

建立实体类对象user

public class user {
    private int id;
    private String name;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

在resources中新建beans.xml


<bean id="user" class="com.ly.spring5.User" >bean>
  • id属性:对象唯一标识
  • class属性:类的全路径(包类路径)

Spring创建对象默认无参构造

基于xml实例化对象(注入属性)

  • set方法注入

<bean id="book" class="com.ly.spring5.Book">
    
    <property name="bname" value="骆驼祥子">property>
    <property name="bname" value="孔乙己">property>
bean>
  • 有参构造注入

<bean id="order" class="com.ly.spring5.Order">
    <constructor-arg name="oname" value="电脑">constructor-arg>
    <constructor-arg name="oaddress" value="china">constructor-arg>
bean>
  • p名称空间注入
xmlns:p="http://www.springframework.org/schema/p"


<bean id="book" class="com.ly.spring5.Book" p:bauthor="ly" p:bname="九阳神功">bean>

基于xml调用对象

/*Dao类实现类*/
package com.ly.spring5.dao;

public class UserDaoImpl implements UserDao{
    @Override
    public void update() {
        System.out.println("dao update........");
    }
}

/*service类*/
package com.ly.spring5.service;

import com.ly.spring5.dao.UserDao;
import com.ly.spring5.dao.UserDaoImpl;

public class UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void add(){
        System.out.println("service add......");
        userDao.update();
    }
}

/*测试类*/
@Test
public void testAdd(){
    //加载Spring配置文件
    ApplicationContext context = new FileSystemXmlApplicationContext("src/bean2.xml");

    //获取配置创建对象
    UserService userService = context.getBean("userService", UserService.class);

    userService.add();
}

基于xml注入集合属性

1、注入数组类型属性
2、注入List集合类型属性
3、注入Map集合类型属性

package com.ly.spring5.collectiontype;

import java.util.List;
import java.util.Map;
import java.util.Set;

public class Stu {
    //数组类型属性
    private String[] courses;

    //List集合类型属性
    private List<String> list;

    //map集合类型属性
    private Map<String,String> maps;

    //set集合类型属性
    private Set<String> sets;

    public void setSets(Set<String> sets) {
        this.sets = sets;
    }

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

    public void setMaps(Map<String, String> maps) {
        this.maps = maps;
    }

    public void setCourses(String[] courses) {
        this.courses = courses;
    }
}

<bean id="stu" class="com.ly.spring5.collectiontype.Stu">
    
    <property name="courses">
        <array>
            <value>JAVAvalue>
            <value>数据库value>
            <value>Springvalue>
            <value>WEBvalue>
        array>
    property>
    
    <property name="list">
        <list>
            <value>张三value>
            <value>小三value>
        list>
    property>    
    
    <property name="maps">
        <map>
            <entry key="JAVA" value="java">entry>
            <entry key="PHP" value="php">entry>
        map>
    property>
    
    <property name="sets">
        <set>
            <value>Mysqlvalue>
            <value>Redisvalue>
        set>
    property>
bean>

xml自动装配


      <bean id="emp" class="com.ly.spring5.autowire.Emp" autowire="byName">

      bean>

      <bean id="dept" class="com.ly.spring5.autowire.Dept">bean>

基于xml连接数据库


<context:property-path path="src/jdbc.properties">context:property-path>


<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
      <property name="driverClassName" value="${jdbc.driverClass">property>
      <property name="url" value="${jdbc.url}">property>
      <property name="username" value="${jdbc.username}">property>
      <property name="password" value="${jdbc.password}">property>
bean>

jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/book?useSSL=FALSE&serverTimezone=UTC
jdbc.username=root
jdbc.password=root

注解方式

基于注解创建对象

  • 引入依赖spring-aop-5.2.5.RELEASE.jar
  • 开启组件扫描

<context:componenet-scan base-package="com.ly">context:componenet-scan>
  • 创建类,在类上面添加创建对象注解
package com.ly.spring5.service;
import org.springframework.stereotype.Component;

//在注解里面value属性值可以省略不写
//默认值是类名称,首字母小写
@Component(value = "userService") //等同于xml开发中的
public class UserService {
    public void add(){
        System.out.println("service add ....");
    }
}

public class demo1 {
    @Test
    public void testService(){
        ApplicationContext context = new FileSystemXmlApplicationContext("src/bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);
        System.out.println(userService);
        userService.add();
    }
}

基于注解实例化对象(注入属性)

package com.ly.spring5.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao{
    @Override
    public void add() {
        System.out.println("dao add....");
    }
}
@Service
public class UserService {
    //定义dao类属性
    //不需要set方法
    //添加注入属性注解
    @Autowired  //根据类型进行注入
    private UserDao userDao;

    public void add(){
        System.out.println("service add ....");
        userDao.add();
    }
}

完全注解开发

  • 创建配置类 ,替代xml配置
@Configuration      //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.ly"})
public class SpringConfig {
    
}
  • 编写测试类
@Testpublic 
void testService2(){    
//加载配置类    
	ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);    
	UserService userService = context.getBean("userService", UserService.class);    
	System.out.println(userService);    
	userService.add();
}

你可能感兴趣的:(JAVA,spring,java,xml)