栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring中的Bean

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring中的Bean

Spring中的Bean
  1. Bean的配置元素:name=id,class ,contructor-arg,ref,scope,value,list,set,map,entry,property
  2. Bean的scope:singleton(默认值,重点),prototype(重点),request,session,application,websocket
  3. Bean的装配方式:基于xml配置文件的装配,基于annotation的装配,基于自动加载的装配方式

注意:如果在Bean中未指定id和name,那么spring会把class值当作id

基于xml配置文件的装配
  1. 设值注入,xml的配置内容如下

        
            
            
            
            
                
                    南昌龙源花园
                    南昌帝豪花园
                
            
        
    

    注意:在bean文件中需要对属性添加set方法,且一定要有一个无参的构造方法

    package com.xyj.demo;
    import java.util.List;
    
    public class SetBean {
    
        private String username;
        private int age;
        private String password;
        private List listHouse;
        public SetBean(){
            super();
        }
        public void setAge(int age) {
            this.age = age;
        }
        public void setPassword(String password) {
            this.password = password;
        }
        public void setListHouse(List listHouse) {
            this.listHouse = listHouse;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        @Override
        public String toString() {
            return "SetBean{" +
                    "username='" + username + ''' +
                    ", age=" + age +
                    ", password='" + password + ''' +
                    ", listHouse=" + listHouse +
                    '}';
        }
    }
    
  2. 构造注入,xml的配置中添加constructor-arg标签,并且需要设置参数位置如index=“0”,表示构造函数的第一个参数,如下:

    
            
            
            
            
                
                    背景
                    背景23阿斯蒂芬
                    背景额地方所发生的
                
            
        
    

    java文件中必须要有与xml文件一样的构造方法。如下:

    package com.xyj.demo;
    
    import java.util.List;
    
    public class ContructorBean {
        private String username;
        private int age;
        private String password;
        private List listHouse;
        public ContructorBean(){
            super();
        }
        public ContructorBean(String username, int age, String password, List listHouse) {
            this.username = username;
            this.age = age;
            this.password = password;
            this.listHouse = listHouse;
        }
    
        @Override
        public String toString() {
            return "ContructorBean{" +
                    "username='" + username + ''' +
                    ", age=" + age +
                    ", password='" + password + ''' +
                    ", listHouse=" + listHouse +
                    '}';
        }
    }
    
基于annotation的装配

使用@Component,@Repository,@Service,@Controller

其中:component,repository,service,controller作用是一样的,只是为了清楚的区分不同的bean的功能或者所属层

@autowired,@Resource,@Qualifier

其中autowired和resource作用是一样的,表示依赖注入,qualifier是配合autowired使用

创建bean1.xml配置文件,使用了****标签




    
    

    
    
    
    


package com.com.xyj.annotationDemo;

import org.springframework.stereotype.Component;

@Component("useDAO")
public class UseDAO {
    public void login(){
        System.out.println("useDao login..");
    }
}


package com.com.xyj.annotationDemo;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("useService")
public class UseService {
    @Resource(name="useDAO")
    private UseDAO useDAO;
    
    public void login(){
        useDAO.login();
        System.out.println("UseService login....");
    }
}

package com.com.xyj.annotationDemo;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("userController")
public class UserController {
    @Resource(name="useService")
    private UseService useService;

    public void save(){
        this.useService.login();
        System.out.println("运行UserController save...");
    }
}

测试类

这里的文件路径需要注意两点:1、是target目录下的路径;2、确定xml在里面,好像Rebuild不会把xml文件放加进去

public class Test {
    private static ApplicationContext applicationContext;
    public static void main(String[] args) {
        //定义配置文件路径
        //在class文件路径下需要有这个文件,才不会抛异常
        //否则提示:Caused by: java.io.FileNotFoundException: class path resource [beans1.xml] cannot be opened because it does not exist
        //这个文件是在target下
//        String path="com/com/xyj/annotationDemo/beans1.xml";
        String path="beans1.xml";
        //获取配置对象
         applicationContext = new ClassPathXmlApplicationContext(path);
        UserController userController = (UserController)applicationContext.getBean("userController");
        userController.save();
    }
}
基于自动加载的装配

创建bean2.xml配置文件,添加了autowire属性,并设置未byName,不会用到注解:

一定要为成员设置set方法,否则会提示空指针:Exception in thread “main” java.lang.NullPointerException



    
    
    

java文件如下:

package com.com.xyj.annotationDemo;

import org.springframework.stereotype.Component;

public class UseDAO {
    public void login(){
        System.out.println("useDao login..");
    }
}
package com.com.xyj.annotationDemo;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

//@Service("useService")
public class UseService {
//    @Resource(name="useDAO")
    private UseDAO useDAO;
//
    public void setUseDAO(UseDAO useDAO) {
        this.useDAO = useDAO;
    }
    public void login(){
        useDAO.login();
        System.out.println("UseService login....");
    }
}

package com.com.xyj.annotationDemo;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;

//@Component("userController")
public class UserController {
//    @Resource(name="useService")
    private UseService useService;

    public void setUseService(UseService useService) {
        this.useService = useService;
    }

    public void save(){
        this.useService.login();
        System.out.println("运行UserController save...");
    }
}

测试类

package com.com.xyj.annotationDemo;

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

public class Test2 {
    public static void main(String[] args) {
        String path ="bean/beans2.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(path);
        UserController userController = (UserController)applicationContext.getBean("userController");
        userController.save();
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/445915.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号