spring

1.Spring依赖注入的方式。

创建Hello实体类

private String name;
private int age;

1.1通过set方法来完成依赖注入。

    <bean id="Hello" class="com.zhiyou100.wyf.dao.Hello" >
        <property name="name" value="张三"/>
        <property name="age" value="11"/>
    </bean>

1.2通过构造方法来完成依赖注入。

<bean id="Hello2" class="com.zhiyou100.wyf.dao.Hello">
        <constructor-arg index="0" value="lisi"/>
        <constructor-arg index="1" value="11"/>
    </bean>

1.3编写测试类

ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        Hello s = (Hello) app.getBean("Hello");
        System.out.println(s);

2.依赖注入的数据类型

2.1基本数据类型和字符串 使用value

2.2如果是指向另一个对象的引用 使用ref

<bean id="Hello3" class="com.zhiyou100.wyf.dao.Hello" autowire="byName">
        <property name="name" value="张三3"/>
        <property name="age" value="113"/>
        <property name="stu" ref="student1"/>    
    </bean>
    <bean id="stu" class="com.zhiyou100.wyf.dao.student">
        <property name="address" value="${user.a}"/>
    </bean>

2.3如果类对象注入的属性类型为list类型。

<bean id="Hello4" class="com.zhiyou100.wyf.dao.Hello">
        <property name="name" value="张三3"/>
        <property name="age" value="113"/>
        <property name="s">
            <list>
                <bean  class="com.zhiyou100.wyf.dao.student">
                    <property name="address" value="南京"/>
                </bean>
                <bean  class="com.zhiyou100.wyf.dao.student">
                    <property name="address" value="北京"/>
                </bean>
                <bean  class="com.zhiyou100.wyf.dao.student">
                    <property name="address" value="九寨沟"/>
                </bean>
            </list>
        </property>
    </bean>

2.4如果类对象注入的属性类型为map类型

<bean id="Hello5" class="com.zhiyou100.wyf.dao.Hello">
        <property name="name" value="张三"/>
        <property name="age" value="11"/>
        <property name="map">
            <map>
                <entry key="wyf1" value="王玉峰"/>
                <entry key="wyf2" value="王玉峰"/>
                <entry key="wyf3" value="王玉峰"/>
            </map>
        </property>
    </bean>

3.bean 的作用域

  Bean的作用域默认为单例模式

  scope:表示bean的作用域,默认singleton, struts框架要求非单例

  prototype:原生,非单例

<bean id="Hello" class="com.zhiyou100.wyf.dao.Hello" scope="prototype">
        <property name="name" value="张三"/>
        <property name="age" value="11"/>
</bean>

4.自动注入

新建两个实体类UserDao和UserService,两个类都实现接口

package com.zhiyou100.wyf.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImp implements UserDao{
    
    public void fingByid(int id) {
        System.out.println("根据id查询数据库"+id);
    }
}
package com.zhiyou100.wyf.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiyou100.wyf.dao.UserDao;
@Service
public class UserServiceImp implements UserService {
    @Autowired
    private UserDao userDao;//依赖注入userdao接口的实现类对象

    public UserDao getUserDao() {
        return userDao;
    }

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    @Override
    public void queryByid(int id) {
        System.out.println("服务层");
        userDao.fingByid(id);
    }
    
    
}
package com.zhiyou100.wyf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.zhiyou100.wyf.service.UserService;
@Controller(value="UserController")
public class UserController {
    @Autowired
    private UserService userService;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    
    public void selectByid(int id) {
        System.out.println("控制台");
        userService.queryByid(id);
    }
    
}

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

<bean id="UserDao" class="com.zhiyou100.wyf.dao.UserDaoImp"></bean>

<bean id="UserService" class="com.zhiyou100.wyf.service.UserServiceImp">
    <property name="userDao" ref="UserDao"></property>
</bean>

<bean id="UserController" class="com.zhiyou100.wyf.controller.UserController">
    <property name="userService" ref="UserService"></property>
</bean>

</beans>

test类

package test;

import org.omg.CORBA.portable.ApplicationException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhiyou100.wyf.controller.UserController;

public class test {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserController u = (UserController) app.getBean("UserController");
        u.selectByid(1);
    }
}

4.2 通过注解

配置文件:

<?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-4.2.xsd">

    <context:component-scan base-package="com.zhiyou100.wyf"></context:component-scan>

</beans>

test类

package test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.zhiyou100.wyf.controller.UserController;

public class test1 {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext1.xml");
        UserController u = (UserController) app.getBean("UserController");
        u.selectByid(2);
    }
}

在相应的类上加上注解.

  @Repository  持久化注解。
  @Service       业务层注解
  @Controller   控制层注解
  @Autowired   自动注入 按照类型帮你自动注入,如果由多个类型相同的那么就会在按照名称注入。(建议使用这个)
  @Resouce     自动注入 按照名称注入,如果没有相同名称的bean那么会按照类型帮你注入。 它可以指定名称来注入

5.在spring配置文件中引入属性文件

users.name=zzzz
users.age=55
users.address=asdasd
1  <context:property-placeholder location="classpath:my.properties"/>
2  <bean id="users" class="com.zhiyou100.wyf.Users">
4      <property name="name" value="${users.name}"></property>
5      <property name="age" value="${users.age}"></property>
6      <property name="address" value="${users.address}"></property>
7  </bean>

不能使用user.name属性,会得到本电脑的用户名

原文地址:https://www.cnblogs.com/yufengwang/p/11478781.html