第一个Spring程序(IOC)

目录

1、导入依赖包以及资源文件导出配置

2、实体类

3、接口

4、接口实现类,这边是多个实现类

5、编写Spring的xml,beans.xml

6、测试类

1、导入依赖包以及资源文件导出配置

<dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

2、实体类

        随意创建一个User实体类

3、接口

public interface UserDao {
    public void getUser();
}

4、接口实现类,这边是多个实现类

        一个接口有多个实现类,将每个实现类都注册进xml里面,后面需要不同实现类的操作就在xml文件里面改就行,不需要改java代码。这个也就是简单的IOC思想

public class UserDaoImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("默认的获得用户");
    }
}
public class UserDaoMysqlImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("mysql获得用户");
    }
}
public class UserDaoOracleImpl implements UserDao{
    @Override
    public void getUser() {
        System.out.println("Oracle获得用户");
    }
}

5、编写Spring的xml,beans.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
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="mysqlImpl" class="com.zhou.dao.UserDaoMysqlImpl"/>
    <bean id="oracleImpl" class="com.zhou.dao.UserDaoOracleImpl"/>
    <bean id="daoImpl" class="com.zhou.dao.UserDaoImpl"/>

    <bean id="UserServiceImpl" class="com.zhou.service.UserServiceImpl">
        <property name="userDao" ref="daoImpl"/>
    </bean>
<!--
ref:引用Spring容器中创建好的对象
value: 具体的值,基本数据类型
-->
</beans>

6、测试类

import com.zhou.dao.UserDaoImpl;
import com.zhou.dao.UserDaoMysqlImpl;
import com.zhou.dao.UserDaoOracleImpl;
import com.zhou.service.UserService;
import com.zhou.service.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyTest {
    public static void main(String[] args) {
//        // 用户实际调用的是业务层,dao层他们不需要接触
//        UserServiceImpl userService = new UserServiceImpl();
//
//        userService.setUserDao(new UserDaoOracleImpl());
//        userService.getUser();
//
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("UserServiceImpl");

        serviceImpl.getUser();

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、安装maven 2、eclipse安装m2eclipse插件 打开Eclipse菜单栏Help->Eclipse Marketplace 输入“Maven”搜索到插件Maven Integration for Eclipse 并点击insall安装 3、安装成成之后我们在Eclipse菜单栏中点击File->New->Other 在弹出的对话框中点击Maven文件夹 点击Maven Project,并点击Next,到下一个对话框是默认设置,继续点击Next到下面这一步 选择maven-archetype-quickstart,点击Next 4、配置pom.xml , 加上以下依赖: <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${springframework.version}</version> </dependency> propertities标签里面加上version信息: <springframework.version>3.2.0.RELEASE</springframework.version> 保存pom.xml后等待下载组件。 5、写代码 如果发现出现applicationContext could not resolve to a type, 可能是依赖没下载完或者下载失败,或者jar包出现问题,解决方法删除这个jar包,然后重新下载。在Eclipse中选中项目右键展开run as,然后选中maven install,重新下载jar包。 https://blog.csdn.net/linxc008/article/details/81054526 applicationContext.xml要加在src/java/resources下 6.bulid project 右击项目,点击Run as, 选择Maven build... , 在弹出的窗口中的Goals栏填上Maven命令:clean package,然后点击Run.然后在有main函数的java文件上右键点击Run As Java Application 注: applicationContext.xml 配置bean信息所依赖的class路径要一致且大小写敏感,否则运行时会出现java.lang.NoClassDefFoundError (wrong name)。 https://blog.csdn.net/my_bkn/article/details/6875481

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值