SoFunction
Updated on 2025-03-08

Solution to inject mapper time null pointer in service layer

Inject mapper time empty pointer in service layer

Today I encountered an extremely tricky problem. I won’t say much nonsense and put the code first.

@RunWith()
@SpringBootTest(classes = SpringBoot_Run.class)
@ContextConfiguration(locations = { "classpath:mybatis/mappers/" })
public class TestTransaction {
 @Autowired
 RevMapper remapper;
 @Test
 public void testInsert() {
  ReData data = new ReData();
  (new Date()).setSeID("fdewfcdsfdssdfdsf").setSendDate(new Date());
  (data);
 }

Then the service code

public class ReService {
 
 @Autowired
 private RevMapper reMapper;
 private Socket socket=null;
 private BufferedReader br=null;
 private PrintWriter pw=null;
 public void recevice() {
  try {
    //Create a server and open port 3081      ServerSocket serv

The RevMapper class is injected well during testing, so that the hair is empty in the service, always empty, empty! ! !

I have added all the @mapperScan and @mapper annotations mentioned online, this is for Mao! ! ! ! !

In the CSDN of all the great masters of the Expo, I found that everyone copied them over, and I admire them! !

solve! ! !

Because I wrote this in the startup class

@SpringBootApplication(exclude=)
@MapperScan(“”)
public class SpringBoot_Run {
public static void main(String[] args) {
 (SpringBoot_Run.class, args);
 ReMapper re=new ReMapper();
 ;
}
}

Awesome Oppas don’t criticize them, this is my first reaction! !

The problem arises. When an object is new, it will not be handed over to spring management, so the object cannot be injected at all. It is natural to null.

The second question is, you want a method to start with the main startup class, you can do this

@Service
public class ReService implements ApplicationRunner{
@Autowired
private RevMapper reMapper;
private Socket socket=null;
。。。。。。。。。。。。。
@Override
public void run(ApplicationArguments args) throws Exception {
 // TODO Auto-generated method stub
 What you need to startXXXXXXXX
}

I feel a little wiser again!

SpringMvc normal class (non-control, service) injection mapper as null

When writing a timer to the project, you need to use an injection mapper for database operations, and use injection in serviceimpl

@Autowired
UserMapper usermapper; 

Invalid. After debugging, it was found that the usemapper is null, which means that the injection was not successful.

After seeing other articles, I learned that the thread from the new is not in the spring container, so it cannot be injected successfully and gets the bean

But according to his method, it is still null. His idea is to actively inject beans, which should be correct.

But my this may be a bit special, so I can only use the ultimate method in the end

ApplicationContext  ac = new ClassPathXmlApplicationContext("classpath:");
usermapper = (UserMapper) ("UserMapper");
();

Don't forget to give the mapper a name, example

@Repository(value="UserMapper")
public interface UserMapper {
public List<User> selectByExample(@Param("username1")String username,@Param("password")String password);
public int insertToken(@Param("username1")String username,@Param("token")String token);
public String checkToken(String token);
public int logout(@Param("username1")String username,@Param("token")String token);
public int deleteAllCookies();
}

This method doesn’t feel very subjectively good, so let’s do it first!

The above is personal experience. I hope you can give you a reference and I hope you can support me more.