Spring---初识IOC(2)

什么是自动装配:

  • 根据指定装配规则(属性名称或者属性类型),Spring自动将匹配的属性值进行注入

实现自动装配步骤:

  • bean标签属性autowire,配置自动装配
  • autowire属性常用两个值:
    byName:根据属性名注入,注入值bean的id值和类属性名称一样
    byType:根据属性类型注入

xml配置文件:

<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 id="student" class="three.ThreeStud" autowire="byName">
        <!--这种方式为手动装配
            <property name="threeTeach" ref="teacher"/>-->
    </bean>
    <!--该id要和bean里面的属性名一样,如果是byName的话。-->
    <bean id="threeTeach" class="three.ThreeTeach"/>
    
</beans>

引入外部属性文件

这里将通过引入数据库连接池配置文件的方式作为演示(druid)。

直接配置数据库信息(xml配置文件):


<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 id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--数据库驱动-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <!--数据库地址 + 数据库名字-->
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <!--连接数据库用户名-->
        <property name="username" value="root"></property>
        <!--连接数据库密码-->
        <property name="password" value="123456"></property>
    </bean>
</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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--引入context名称空间-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!--引入外部属性文件-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--数据库驱动-->
        <property name="driverClassName" value="${prop.Name}"></property>
        <!--数据库地址 + 数据库名字-->
        <property name="url" value="${prop.url}"></property>
        <!--连接数据库用户名-->
        <property name="username" value="${prop.username}"></property>
        <!--连接数据库密码-->
        <property name="password" value="${prop.password}"></property>
    </bean>

</beans>

外部配置文件:

步骤:

  • 1,创建外部属性文件,jdbc.properties
  • 2,把外部properties属性文件引入到spring配置文件中

(键值key是自己定的)

prop.Name=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/test
prop.username=root
prop.password=123456

在这里插入图片描述


扩展内容1:使用注解完成Bean管理

  • 使用注解目的:简化xml配置

Spring针对Bean管理中创建对象提供的注解:

  • 1,Component
  • 2,Service
  • 3,Controller
  • 4,Repository

上面四个注解功能是一样的,都可以用来创建bean实例

步骤:
1,开启扫描

扫描的意思是,比如一部分的类,查看这一部分类是否符合规则。让注解生效

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

    <!--开启组件扫描
        1,如果扫描多个包,多个包使用逗号隔开
        2,扫描包上层路径
        等同于将five包内所有类都进行扫描-->
    <context:component-scan base-package="five"></context:component-scan>

</beans>

按照上面的写法,也就是five目录包含的所有类无论是什么注解(@Component,@Service…)都进行bean对象的创建,等同于全部都进行xml配置。

所以有时候要进行只是配置@Component注解时,则需要过滤掉其他的注解
(记得要引入名称空间)

  • 过滤的两种写法:
    <!--
        use-default-filters:表示现在不使用默认的filter,自己配置filter
        context:include-filter:设置扫描内容
    -->
    <context:component-scan base-package="five" use-default-filters="false">
        <!--type:根据什么扫描(annotation:注解)  expression:只扫描带有Controller注解的-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>


    <context:component-scan base-package="five" use-default-filters="false">
        <!--不扫描Controller-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

context名称空间:

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

</beans>

2,创建类,在类上面添加创建对象注解

//在注解里面value属性值可以省略不写,默认值是类名称,首字母小写
@Component(value = "fiveClass") //<bean id="fiveClass" class=".." ..../>
public class FiveClass {
    private String name;

    public void method(){
        System.out.println("FiveClass method");
    }
}

基于注解的方式进行属性注入

Spring提供的注解:
  • 1,Autowired:根据属性类型进行自动装配

  • 2,Qualifier:根据属性名称进行自动注入
    需要和@Autowired一起使用
    Qualifier(value=“类名称”)

  • 3,Resource:可以根据类型注入,也可以根据名称进行注入

  • 4,Value:注入普通类型注入

实现:

@Autowired(根据属性类型进行注入):

@Component
public class FiveStud {
	
    //不需要添加set方法
    @Autowired
    private FiveTeach fiveTeach;
    
}

@Qualifier(根据属性名称进行自动注入):

@Component
public class FiveStud {
    
    //需要和Autowired一起用,value值为类上面的注解value值
    @Autowired
    @Qualifier(value = "fiveTeach")
    private FiveTeach fiveTeach;
    
}

@Resource(两种方式注入都可以,该注解不是Spring的,而是javax的):

@Component
public class FiveStud {
    
	 /*
        @Resource是javax的,不是spring的,所以spring更推荐使用Autowired和Qualifier
     */

    //表示按照类型注入
    //@Resource
    //private FiveTeach fiveTeach;
    
    //表示按照名称注入
    @Resource(name = "fiveTeach")
    private FiveTeach fiveTeach;
   
}

值得注意的是:上面的几种注解方式都不能用于普通数据类型。

@Value(能用于普通数据类型):

@Component
public class FiveStud {

   
    @Value("abc")
    private String name;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
Spring-IOCSpring框架的核心部分之一,它是一种设计模式,全称为Inversion of Control(控制反转)。它通过将对象的创建、依赖关系的管理和对象的生命周期交给Spring容器来实现,从而降低了组件之间的耦合度,提高了代码的可重用性和可维护性。Spring-IOC的实现主要依靠Spring容器,Spring容器是Spring框架的核心,它负责创建、管理和装配Bean对象,其中Bean是Spring框架中最基本的组件。 Spring-IOC的实现主要有两种方式:BeanFactory和ApplicationContext。其中,BeanFactory是Spring-IOC的基本实现,而ApplicationContext是BeanFactory的子接口,提供了更多高级特性。ApplicationContext是Spring框架中最常用的IOC容器,它除了提供BeanFactory的所有功能外,还提供了更多的企业级特性,例如AOP、事务管理、国际化、事件传播等。 下面是一个简单的Spring-IOC的例子,假设我们有一个UserService接口和一个UserServiceImpl实现类,我们可以通过Spring-IOC容器来创建和管理UserServiceImpl对象: 1.定义UserService接口和UserServiceImpl实现类 ```java public interface UserService { void addUser(User user); } @Service public class UserServiceImpl implements UserService { @Override public void addUser(User user) { // 添加用户的具体实现 } } ``` 2.在Spring配置文件中配置UserService实例 ```xml <bean id="userService" class="com.example.service.UserServiceImpl"/> ``` 3.在代码中获取UserService实例并使用 ```java ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); UserService userService = context.getBean("userService", UserService.class); User user = new User(); userService.addUser(user); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值