对于IOC的注入两种方式(注解和XML)

xml进行注入

在Spring IoC 容器中基于xml完成对Bean的装配,创建测试类,获取ProductController的实例,调用其save()方法,在控制台上打印Product的信息

spring-config.xml
java 复制代码
<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:aop="http://www.springframework.org/schema/aop"

       xsi:schemaLocation="

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    
        <bean id="productController" class="com.yt.Controller.ProductController">



        </bean>


    </beans>
ProductTest
java 复制代码
@Test

public void test2(){


    Product product = new Product(12, "111", "111", 123);
//这里是要根据target的相对路径来获取的相应的xml文件的的位置的

    ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

    ProductController productController = (ProductController) classPathXmlApplicationContext.getBean("productController");

    productController.save(product);

}

注解注入

在Spring IoC 容器中基于注解(@Autowired)完成对Bean的装配,创建测试类,获取ProductController的实例,调用其save()方法,在控制台上打印Product的信息

AppConfig
java 复制代码
package com.yt.config;



import org.springframework.context.annotation.ComponentScan;



/**

 * @program: mybatis-operate

 * @description

 * @author: YangTao

 * @create: 2024-04-22 08:43

 **/

@ComponentScan("com.yt")

public class AppConfig {

}
Product
java 复制代码
package com.yt;



/**

 * @program: mybatis-operate

 * @description

 * @author: YangTao

 * @create: 2024-04-22 08:25

 **/

public class Product {

    private Integer id;

    private String name;

    private String subTitle;

    private float price;



    public Product(Integer id, String name, String subTitle, float price) {

        this.id = id;

        this.name = name;

        this.subTitle = subTitle;

        this.price = price;

    }



    public Integer getId() {

        return id;

    }



    public void setId(Integer id) {

        this.id = id;

    }



    public String getName() {

        return name;

    }



    public void setName(String name) {

        this.name = name;

    }



    public String getSubTitle() {

        return subTitle;

    }



    public void setSubTitle(String subTitle) {

        this.subTitle = subTitle;

    }



    public float getPrice() {

        return price;

    }



    public void setPrice(float price) {

        this.price = price;

    }



    @Override

    public String toString() {

        return "Product{" +

                "id=" + id +

                ", name='" + name + '\'' +

                ", subTitle='" + subTitle + '\'' +

                ", price=" + price +

                '}';

    }

}
Controller
java 复制代码
package com.yt.Controller;



import com.yt.Product;

import com.yt.Service.ProductService;

import org.springframework.stereotype.Controller;



/**

 * @program: mybatis-operate

 * @description

 * @author: YangTao

 * @create: 2024-04-22 08:27

 **/

@Controller

public class ProductController {

        private ProductService productService;



        public void save(Product product){

            System.out.println("hello");

//            productService.save(product);// 调用业务层接口的保存方法

        }

}
Service
java 复制代码
package com.yt.Service;



import com.yt.Product;

import org.springframework.stereotype.Service;



/**

 * @program: mybatis-operate

 * @description

 * @author: YangTao

 * @create: 2024-04-22 08:28

 **/

public interface ProductService {

    public void save(Product product); // 保存商品操作

}









package com.yt.Service.ServiceImpl;



import com.yt.Product;

import com.yt.Service.ProductService;

import org.springframework.stereotype.Service;



/**

 * @program: mybatis-operate

 * @description

 * @author: YangTao

 * @create: 2024-04-22 08:28

 **/

@Service

public class ProductServiceImpl implements ProductService {

    @Override

    public void save(Product product) {

        System.out.println(product); // 实现接口中定义的方法

    }

}
ProductTest
java 复制代码
public class ProductTest {



    @Test

    public void test1(){

        Product product = new Product(12, "111", "111", 123);

        AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext(AppConfig.class);

        ProductController productController = (ProductController) annotationConfigApplicationContext.getBean(ProductController.class);

        productController.save(product);





        ProductService productService = (ProductService) annotationConfigApplicationContext.getBean(ProductService.class);

        productService.save(product);

    }

}
相关推荐
Q_Q19632884752 小时前
python的漫画网站管理系统
开发语言·spring boot·python·django·flask·node.js·php
MaCa .BaKa2 小时前
38-日语学习小程序
java·vue.js·spring boot·学习·mysql·小程序·maven
Uranus^3 小时前
深入解析Spring Boot与Redis集成:高效缓存实践
java·spring boot·redis·缓存
椰椰椰耶3 小时前
【RabbitMQ】整合 SpringBoot,实现工作队列、发布/订阅、路由和通配符模式
spring boot·rabbitmq·java-rabbitmq
Uranus^4 小时前
使用Spring Boot和Spring Security构建安全的RESTful API
java·spring boot·spring security·jwt·restful api
编程乐学(Arfan开发工程师)4 小时前
06、基础入门-SpringBoot-依赖管理特性
android·spring boot·后端
编程乐学(Arfan开发工程师)4 小时前
05、基础入门-SpringBoot-HelloWorld
java·spring boot·后端
拾贰_C4 小时前
【SpringBoot】关于MP使用中配置了数据库表前缀的问题
数据库·spring boot·oracle
橘子海全栈攻城狮4 小时前
【源码+文档+调试讲解】党员之家服务系统小程序1
java·开发语言·spring boot·后端·小程序·旅游
冼紫菜5 小时前
Java开发中使用 RabbitMQ 入门到进阶详解(含注解方式、JSON配置)
java·spring boot·后端·rabbitmq·springcloud