【SSM入门(二)】:setter依赖注入【超简单】

导读:本篇文章讲解 【SSM入门(二)】:setter依赖注入【超简单】,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

目录

🌞测试结果

🌞实现步骤

(1)建立空项目 ​编辑

(2)建立一个模块:maven项目

(3) 建立数据层dao和业务层service的接口和实现类文件

(4)UserDao接口

(5)BookDaoimpl.class

(6)建立测试类app.class

(7)在pom.xml导入spring的两个坐标

(8)在pom.xml导入spring的两个坐标

(9)建立配置文件applicationContext.xml


 🌞哈喽,大家好丫,你们的小郭子又来啦 ~

🌞今天我们继续聊一聊SSM中的【setter依赖注入】,

话不多说,直接上干货,嘻嘻嘻 ~

                        【SSM入门(二)】:setter依赖注入【超简单】【SSM入门(二)】:setter依赖注入【超简单】

🍥 🍙测试结果

【SSM入门(二)】:setter依赖注入【超简单】

🍥 🍙实现步骤

(1)建立空项目 【SSM入门(二)】:setter依赖注入【超简单】

(2)建立一个模块:maven项目

【SSM入门(二)】:setter依赖注入【超简单】

(3) 建立数据层dao和业务层service的接口和实现类文件

【SSM入门(二)】:setter依赖注入【超简单】

(4)UserDao接口

package com.itheima.dao;
 
public interface UserDao {
    public void save();
}

(5)BookDaoimpl.class

package com.itheima.dao.impl;
 
import com.itheima.dao.BookDao;
 
public class BookDaoimpl implements BookDao {
    private String name;
    private  int age;
 
    public void setName(String name) {
        this.name = name;
    }
 
    public void setAge(int age) {
        this.age = age;
    }
 
    public void save() {
        System.out.println("BookDao运行了"+name+","+age);
    }
//测试生命周偶器
    public void init(){
        System.out.println("初始化成功");
    }
    public void destroy(){
        System.out.println("程序关闭");
    }
}

(6)建立测试类app.class

package com.itheima;
 
import com.itheima.dao.BookDao;
import com.itheima.service.BookService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class App3 {
    public static void main(String[] args) {
        //获取ioc容器
        ClassPathXmlApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
        //setter注入方法
        BookService service = (BookService) ctx.getBean("bookservice1");
        service.save();
//依赖方式
 
 
 
    }
}

(7)在pom.xml导入spring的两个坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>springioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
 
</project>

(8)在pom.xml导入spring的两个坐标

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
 
    <groupId>org.example</groupId>
    <artifactId>springioc</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.10.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
 
 
</project>

(9)建立配置文件applicationContext.xml

<?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">
 
<!--    配置bean、-->
<!--    起别名name,作用范围prototype
-->
    <bean id="bookdao1" name="aa bb cc" class="com.itheima.dao.impl.BookDaoimpl" init-method="init" destroy-method="destroy" >
 
        <property name="name" value="郭浩康"></property>
        <property name="age" value="20"></property>
    </bean>
    <bean id="userdao1" class="com.itheima.dao.impl.UserDaoimpl"/>
    <bean id="bookservice1"  name="kk" class="com.itheima.service.impl.BookServiceimpl">
 
<!--                配置service和dao发生关系-->
<!--        <property name="book" ref="aa"></property>-->
        <property name="book" ref="aa"></property>
        <property name="userDao" ref="userdao1"></property>
<!--        <property name="book" ref=bb"></property>-->
    </bean>
</beans>

好啦,今天的分享到这里就结束啦 ~🌞🌞

觉得我分享的文章不错的话,可以关注一下哦,嘻嘻嘻🌞🌞

          【SSM入门(二)】:setter依赖注入【超简单】               【SSM入门(二)】:setter依赖注入【超简单】         【SSM入门(二)】:setter依赖注入【超简单】     

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/73234.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!